PHP Socket Server vs node.js: Web Chat PHP Socket Server vs node.js: Web Chat php php

PHP Socket Server vs node.js: Web Chat


JavaScript, or in this case V8 which is the engine that Node is using, is by design single threaded. So yes there's just an event queue.

But in the end, that's not a problem, something's always gonna happen first, unless you're using multiple processors, and even then, you will most likely only have one network card... one router... you get the idea. Also, using 1000+ threads... not a good idea, scales badly, and you will find yourself in a concurrency HELL.

1000 chat users, that will be no problem at all for Node.js.

I can give you a pretty basic idea how you would set it up, this plain vanilla chat thingy works over telnet, it has.. no features, but it works:

var net = require('net'); // require the net modulevar users = []; // keep track of the users// setup a new tcp socket servernet.createServer(function(socket) { // provide a callback in case a new connection gets                                    // established, socket is the socket object    // keep track of this users names, via use of closures    var name = '';    // ask the new user for a name    socket.write('Enter a Name(max 12 chars): ');    // register a callback on the socket for the case of incoming data    socket.on('data', function(buffer) { // buffer is a Buffer object containing the data        if (name !== '') {  // in case this user has a name...            // send out his message to all the other users...            for(var i = 0; i < users.length; i++) {                if (users[i] !== socket) { // ...but himself                    users[i].write(name + ': '                                   + buffer.toString('ascii').trim()                                   + '\r\n');                }            }        // otherwise take the data and use that as a name        } else {            name = buffer.toString('ascii').substring(0, 12).trim().replace(/\s/g, '_');            socket.write('> You have joined as ' + name + '\r\n');            // push this socket to the user list            users.push(socket);            for(var i = 0; i < users.length; i++) {                if (users[i] !== socket) {                    users[i].write('> ' + name + ' has joined' + '\r\n');                }            }        }    });    // another callback for removing the user aka socket from the list    socket.on('end', function() {        users.splice(users.indexOf(socket), 1);    });// bind the server to port 8000}).listen(8000);

There's no magic involved in here (besides the use of a closures), you don't have to do with raw socket programming and you won't have any concurrency problems. And you learn some of the latest hotness ;)

I recommend that you watch some of the talks that are listed on our Node.js tag wiki, to get a better grasp of how Node.js works.


Another topic which I answered which could help you and will easily scale to your needs => How to use redis PUBLISH/SUBSCRIBE with nodejs to notify clients when data values change?