Chat project - load balance with socket.io Chat project - load balance with socket.io nginx nginx

Chat project - load balance with socket.io


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


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.