Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);

    redis.auth(rtg.auth.split(":")[1]);
} 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 stores in the lib directory:

enter image description here


EDIT

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.

like image 314
OdieO Avatar asked Jun 30 '14 21:06

OdieO


2 Answers

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');

io.adapter(redis(process.env.REDISTOGO_URL));

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

like image 140
generalhenry Avatar answered Sep 20 '22 13:09

generalhenry


I had to parse libraries above to get this example, so I figured I would post a full blown example, but I must admit there are a couple of things off, this uses REDISCLOUD, it is on Heroku, it does work. I'll post this elsewhere and maybe put it in a doc too.

var redis = require('redis');
var ioredis = require('socket.io-redis'); //Adapter
var url = require('url'); 
var redisURL = url.parse(process.env.REDISCLOUD_URL );

var pub = redis.createClient(redisURL.port, redisURL.hostname, {return_buffers: true});
var sub = redis.createClient(redisURL.port, redisURL.hostname, {return_buffers: true});
pub.auth(redisURL.auth.split(":")[1]);
sub.auth(redisURL.auth.split(":")[1]);

var redisOptions = {
  pubClient: pub,
  subClient: sub,
  host: redisURL.hostname,
  port: redisURL.port
};

io.adapter(ioredis(redisOptions));
like image 28
Jeff Ancel Avatar answered Sep 23 '22 13:09

Jeff Ancel