We are going to be using Node.js’s events library to listen for friend changes from within our Socket.io channel. This gets us around having to subscribe to multiple channels (which isn’t possible) and opens an avenue for scaling out the chat function using something like Redis or RabbitMQ.
This command tells you how many sockets are in a room:
Why are custom events so important for real-time notification in the social networking application? After all, you could very easily trigger an event every time someone logs in, updates his status, or comments on someone else’s profile, and let the event handler decide which connected sockets should see the event.
Because Node.js is single-threaded, any logic you do in your event handler will effectively block your running code. So while it might be fast to process a login event if you have only two connections open from your development computer, if you have a real running site your server will be spending a lot of time going through all of the connections to figure out who a particular message should be sent to. That would be a lot of wasted time when it is just as easy ...