node.js running alongside Apache PHP? node.js running alongside Apache PHP? apache apache

node.js running alongside Apache PHP?


I suggest you use Socket.io along side node.js. Install and download the libs from http://socket.io/. You can run it along side your Apache server no problems.

First create a node server:

var http = require('http')  , url = require('url')  , fs = require('fs')  , io = require('../')//path to your socket.io lib  , sys = require(process.binding('natives').util ? 'util' : 'sys')  , server;server = http.createServer(function(req, res){  var path = url.parse(req.url).pathname;}),server.listen(8084);//This could be almost any port number

Second, run your server from the command line using:

node /path/to/your/server.js

Third, connect to the socket using client side js:

var socket = new io.Socket(null, {port: 8084, rememberTransport: false});socket.connect();

You will have to have include the socket.io lib client side aswell.

Send data from client side to the node server using:

socket.send({data:data});

Your server.js should also have functions for processing requests:

io.on('connection', function(client){//action when client connets client.on('message', function(message){    //action when client sends msg  });  client.on('disconnect', function(){    //action when client disconnects  });});

There are two main ways to send data from the server to the client:

client.send({ data: data});//sends it back to the client making the request

and

client.broadcast({  data: data});//sends it too every client connected to the server


I suspect the chat as well as the logged in listing would work via Ajax.

The chat part would be pretty easy to program in Node.js, use one of the mysql modules for Node to connect to your existing database and query login information and such and then do all the actual chatting via Node.js, I recommend you to check out Socket.io since it makes Browser/Node.js communcation really trivial, this should allow you to focus on the actual chat logic.

Also, you could take a look at the "official" chat demo of Node.js, for some inspiration.

As far as the currently online part goes, this is never easy to implement since all you can do is to display something along the lines of "5 users active in the last X minutes".

Of course you could easily add some Ajax that queries the chat server and display the userlist from that on the homepage.

Or you completely crazy and establish a Socket.io connection for every visitor and monitor it this way, although it's questionable whether this is worth the effort.