0

The general rule in writing Node.js code is that all code should be non-blocking and communicate via events. I would like to know if this code written using the Socket.IO library for Node.js create a blocked connection, or does it follow the general Node.js rules?

sio.sockets.on('connection', function (socket) {
  socket.on('message', function (msg) {
    console.log("Received message"+message);
  });

  socket.on('cookie', function (msg) {
    console.log("Cookie Received");
    console.log(msg);
  });

  this.send('hello');


  socket.on('disconnect', function (){
    console.log('Disconnected');
  });
});

Would be grateful for any help.

n3m6
  • 1
  • 2
  • It sets up a server, just like http.createServer( function ) – generalhenry Jul 12 '11 at 02:28
  • 1
    You can't do any blocking(okay you can, but that is just stupid => while(true) ;) when coding javascript. But when you create c extension you can program blocking code which is bad! – Alfred Jul 12 '11 at 13:36

1 Answers1

2

no, node.js socket.io server listens tcp with standard node non-blocking api

That is, your control goes immediately to next statement after sio.sockets.on(..);, the only thing happen in this call is 'construct javascript function object abd assign to listeners array'.

Andrey Sidorov
  • 24,905
  • 4
  • 62
  • 75