15

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?

Renan Basso
  • 795
  • 2
  • 7
  • 19

2 Answers2

15

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...

Brian Noah
  • 2,962
  • 18
  • 27
  • How can I use AWS ELB? – Renan Basso Feb 14 '15 at 03:55
  • What I need to do to configure redis server? – Renan Basso Feb 14 '15 at 04:29
  • in order to use AWS ELB, you need an AWS account where you are hosting your servers. You can forward your ports via the ELB to the different servers. It's pretty cool. As far as Redis configuration goes, that's so easy to set up. It's pretty much plug and play. Have it on a central server that is firewalled with your other servers. – Brian Noah Feb 14 '15 at 21:50
  • Hi @BrianNoah I have a question regarding this answer. I have implemented the exact code, you have above. Is this all that is needed for running socket.io behind a load balancer? If so, whats the best way to actually test if its working correctly? – Dev123Dev Mar 07 '16 at 23:48
  • @Srmuhs i have implemented the same architecture. it works when i broadcast but it fails when i emit on specific socket ids like in one on one chatiing especially when its generated from other server behind load balancer. – Asif Saeed Feb 10 '17 at 18:41
  • What about nginx's IP Hash load balancing method? http://joecaps.com/blog/2017/01/23/load-balancing-nodejs-instances-using-nginx/. Would that allow me to continue using just nginx & node while being able to scale up more node servers without breaking my socket connections? – cs_pupil Apr 27 '19 at 22:13
3

Check out the NPM package mong.socket.io . It has the ability to save socket.io data to mongoDB like below;

{
    "_id" : ObjectId("54b901332e2f73f5594c6267"),
    "event" : "join",
    "message" : {
            "name" : "join",
            "nodeId" : 426506139219,
            "args" : "[\"URAiA6mO6VbCwquWKH0U\",\"/54b6821asdf66asdasd2f0f9cd2997413780273376\"]"
    }}

Or you may use the redis adapter as mentioned there;

Socket.IO Using multiple nodes

Then just use the NGINX reverse proxy and all of the node processes should share Socket.IO events with each other.

cdagli
  • 1,578
  • 16
  • 23