如何将Node.js服务器连接到Apache服务器?

I'd like to use Socket.io and Node.js to do push notifications. The end goal is to do something similar to what Stackoverflow does with their commenting to notify people of new comments.

My site is in PHP and runs on Apache on an EC2 instance. I've heard that Apache doesn't handle concurrency well so I'm interested in using Node to handle the hopefully large # of simultaneous, persistent connections. I guess there are two solutions:

  1. Put Node on same instance as Apache and proxy the two servers
  2. Put Node on a separate instance

In either case, I don't know how the connection between these two servers looks programmatically. For example, while WebSockets/Node handles the sending/receiving of messages, I need to store these messages in my MySQL database and that would require some PHP code, yes/no? Also, how is the message received on my PHP page? Via a $.post to a url like this, http://mysite/receiver.php that would be resolved by my Apache server?

I'd be happy to see either comments or code to help me understand this better.

  • You can run different servers at different ports.
  • In order for the servers to communicate in a way you can use Sessions/MySQL/flat files/Cache/json/xml...
  • You can store messages wherever you want there is no limitation on that, MySQL module for node.js exists, memcache module for node.js exists and many more, PHP code is not required, its optional to query the database.
  • The messages on your PHP page are fetched from your database and rendered, if node.js stores stuff in mysql server you can fetch them with PHP or temporarily store to an array and push messages back to client while you store into database for new users to have "current" content.
  • Avoid using ajax since you can stream data to client via websockets in realtime use when needed.

-- Edited
Some additional information on How to use vhosts alongside node-http-proxy? link provided from Timothy Strimple at comments , thanks

Some MySQL drivers can be found in this question What MySQL drivers are available for node.js?

About configuring ports:
-On apache you need to manually edit httpd.conf to define default port
-On node.js things are simpler and you can define a port in code Using node.js as a simple web server

   var connect = require('connect');
    connect.createServer(
     // .. code
    ).listen(PORT);

hope it helped

I need to store these messages in my MySQL database and that would require some PHP code, yes/no?

No. Node allows you to write javavscript on the server and you can do pretty much anything with it that you would do with PHP, including connect to a MySQL database.

You can run both Node and Apache on the same server and use NGINX to proxy between the two, or you could use Node to proxy to your Apache. But don't use Apache to proxy to Node as you'd be hurting performance.

Here are some links that will hopefully help:

You could also use sockets as a direct means of communication between the two using node.js's net module and php's socket module. Here is an example.

Simple node js server listening for a connection

var net = require('net');

var listenport = 3000; // Should be the same as in the php script

// Set up server
var Server = net.createServer(function(Sock) {
    console.log('Client Connected.');

    Sock.on('data', function(data) {
       console.log('Data received: ' + data);

       dataobj = JSON.parse(data);

       console.log('Item ID: ' + dataobj.itemid);

       // and so on
    });

    Sock.on('end', function() {
        console.log('Client Disconnected.');
    });

    Sock.pipe(Sock);
});

Server.listen(listenport, function() {
   console.log('Listening on port ' + listenport); 
});

Simple PHP connecting to the node instance

<?php
error_reporting(E_ALL);

$port = 3000; // Port the node app listens to
$address = '127.0.0.1'; // IP the node app is on

// Create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "
";
}

// Connect to node app
$result = socket_connect($socket, $address, $port);
if ($result === false) {
    echo "socket_connect() failed.
Reason: ($result) " . socket_strerror(socket_last_error($socket)) . "
";
}

// Data we want to send
$data = array('itemid' => '1234567', 'steamid' => '769591951959', 'otherinfo' => 'hi there');

// Prepares to transmit it
$encdata = json_encode($data);

socket_write($socket, $encdata, strlen($encdata));

socket_close($socket);

echo 'Sent data
';

?>

original source: github how to connect php to node js