Chat project - load balance with socket.io

Renan Basso picture Renan Basso · Feb 8, 2015 · Viewed 10.4k times · Source

I am involved in a development project of a chat where we are using node.js, socket.io (rooms) and mongodb. We are at the stage of performance testing and we are very concerned if the system needs a load balance.

How can we develop if our project needs it? J'a researched on NGINX looks cool, but we are in doubt whether solves our problem as how the system will be a chat, we fear the servers are not ~talking~ with each other correctly ...

Where do we go if we need a load balancing?

Answer

Brian Noah picture Brian Noah · Feb 13, 2015

To ensure that we can scale to multiple nodes but keep up interconnectivity between different clients and different servers, I use redis. It's actually very simple to use and set up.

What this does is creates a pub/sub system between your servers to keep track of your different socket clients.

var io = require('socket.io')(3000),
    redis = require('redis'),
    redisAdapter = require('socket.io-redis'),
    port = 6379,
    host = '127.0.0.1',
    pub = redis.createClient(port, host),
    sub = redis.createClient(port, host, {detect_buffers: true}),
    server = http(),
    socketServer = io(server, {adapter: redisAdapter({pubClient: pub, subClient: sub})});

read more here: socket.io-redis

As far as handling the different node servers, there are different approaches.

  • AWS ELB(elastic load balancer)
  • Nginx
  • Apache
  • HAProxy

Among others...