Scaling up the application

In the previous section we saw how we can cluster a Node.js app and how it remains restricted due to our application mechanisms. In its current state, the application uses an in-memory store to keep the session data. This store is local to the Node.js instance and so won't be accessible in any another clustered instance. Also, the data will be lost in a Node.js instance restart. So, what we need is a way to store the session in a persistent store. Also, we want to configure socket.io such that all its instances use a shared pub-sub and data store. The Connect framework has an extension mechanism so a new store can be plugged in, and there is one store that is persistent as well as excels at pub-sub. It is the Redis Session Store.

Redis (http://redis.io/) is a high performance, distributed, open source key-value store that can also be used as a queue. We will use Redis and corresponding Redis stores to provide a reliable, distributed, and shared store and pub-sub queue. Please check out the instructions to install the Redis server on your operating system and start it up.

Let's make a few changes to our chat application, beginning with package.json:

    "connect-redis":"*",
    "redis":"*"

This will add support for the Connect/Express.js Redis store and the Redis connection client. Let's first get Express.js to use Redis; to do so, edit app.js by referring to the following code snippet:

var express = require('express')
  , routes = require('./routes')
  , http = require('http')
  , path = require('path')
  , connect = require('connect')
  , RedisStore = require('connect-redis')(express);

var app = express();

var sessionStore = new RedisStore();

//Existing Code

So the two changes we make here are pulling in the Redis session store and then we can replace the session store to be an instance of RedisStore. That's all that is needed to get Express running using the Redis store.

The next thing we need to do is get socket.io using Redis. So, let us edit socket.js:

var io = require('socket.io')
  , redis = require('redis')
  , RedisStore = require('socket.io/lib/stores/redis')
  , pub    = redis.createClient()
  , sub    = redis.createClient()
  , client = redis.createClient();

exports.initialize = function (server) {
  io = io.listen(server);

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

  //Existing Code
}

The first thing in the preceding code snippet that we are doing is require ('redis'), which provides the client and redisStore from socket.io, which provides redis backed for socket.io. Then we create three different Redis clients to use for pub-sub and the data store:

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

In the previous code snippet, we configure socket.io to use Redis for the queue and data store. And we are ready to go! Now run the application again using the following command:

npm start
..................Content has been hidden....................

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