Ramsel Ramsel - 1 year ago 217
Node.js Question

How to set RedisStore -- Node, Express, Socket.io, Heroku

I'm using Node & Express 4.0 deployed on Heroku, and I'm trying to implement Socket.io with Redis as aa session store. So I have this as my current code:

var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var RedisStore = io.RedisStore;

if (process.env.REDISTOGO_URL) {
// inside if statement
var rtg = require("url").parse(process.env.REDISTOGO_URL);
var redis = require("redis").createClient(rtg.port, rtg.hostname);

} else {
var redis = require("redis").createClient();

/** Initialize RedisStore for socket.io **/
io.set('store', new RedisStore({
redis : redis

But I get the following error:

14:25:03 web.1 | io.set('store', new RedisStore({
14:25:03 web.1 | ^
14:25:03 web.1 | TypeError: undefined is not a function

I've also seen this way of defining a RedisStore:

var redis = require('socket.io/node_modules/redis');
var RedisStore = require('socket.io/lib/stores/redis');

However, my installed version of socket.io, installed using
npm install --save socket.io
, doesn't include
in the

enter image description here


I saw this on the socket.io page in regards to their 1.0 release:

// 2. Implement the socket.io-redis adapter

var io = require('socket.io')(3000);
var redis = require('socket.io-redis');
io.adapter(redis({ host: 'localhost', port: 6379 }));

But there's no other documentation I could find regarding this new module, and since I'm new to this whole stack, I don't think I could figure it out on my own.

Answer Source

The trend among node.js modules is to remove functionality that isn't truly core to the module.

Which is why socket.io 1.0 no longer supports redis out of the box.

So step one is to track down the functionality you need.

  1. http://socket.io/docs/server-api/
  2. https://github.com/Automattic/socket.io-adapter
  3. https://github.com/Automattic/socket.io-redis

Then you need to install the other module npm install socket.io-redis --save

And finally configure your app.

var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var redis = require('socket.io-redis');


The nice part is the socket.io-redis adapter accepts redis urls and defaults to localhost:6379 so you (should) be able to simple pass in the REDISTOGO_URL

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download