Scaling Socket.IO with Redis

Socket.IO also uses an in-memory store to store its events. There are a couple of issues with this; the first being that if the server fails we lose those messages stored in memory. The second is if we attempt to scale our application by adding more servers, the Socket.IO in-memory store will be tied to a single server; the servers we add will not know which Socket.IO connections are open on other servers.

We can solve these problems by using the Socket.IO RedisStore. We start by requiring a RedisStore, which is a redis module from the Socket.IO namespace. We can also use the vision Redis module to create three redis clients: pub, sub, and client. In order to configure Socket.IO to use the RedisStore, we set the Socket.IO 'store' to a RedisStore, which passes redis, pub, sub, and client as the arguments.

var config = require('../configuration')
, RedisStore = require('socket.io/lib/stores/redis')
, redis  = require('socket.io/node_modules/redis')
, Redis = require('../cache/redis')
, pub    = new Redis().client
, sub    = new Redis().client
, client = new Redis().client;

function Socket(server) {
    /....

    socketio.set('store', new RedisStore({
      redis    : redis
    , redisPub : pub
    , redisSub : sub
    , redisClient : client
    }));

    return socketio;
};
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.98.18