Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJS + SocketIO: Scaling and preventing single point of failure

So the first app that people usually build with SocketIO and Node is usually a chatting app. This chatting app basically has 1 Node server that will broadcast to multiple clients. In the Node code, you would have something like.

//Psuedocode
for(client in clients){
  if(client != messageSender){
    user.send(message);
  }
}

This is great for a low number of users, but I see a problem with this. First of all, there is a single point of failure which is the Node server. Second of all, the app will slow down as the number of clients grow. What is there to do then when we reach this bottleneck? Is there an architecture (horizontal/vertical scaling) that can be used to alleviate this problem?

like image 536
denniss Avatar asked Mar 16 '12 05:03

denniss


1 Answers

For that "one day" when your chat app needs multiple, fault-tolerant node servers, and you want to use socket.io to cross communicate between the server and the client, there is a node.js module that fits the bill.

https://github.com/hookio/hook.io

It's basically an event emitting framework to cross communicate between multiple "things" -- such as multiple node servers.

It's relatively complicated to use, compared to most modules, which is understandable since this is a complex problem to solve.

That being said, you'd probably have to have a few thousand simultaneous users and lots of other problems before you begin to have problems with this.

Another thing you can do, is try to develop your application in a way so that if a connection is lost (which happens all the time anyway), eg. server goes down, client has network issues (eg. mobile user), etc, your application should be able to handle that and recover from such issues gracefully.

like image 152
arnorhs Avatar answered Sep 20 '22 00:09

arnorhs