How to set RedisStore -- Node, Express, Socket.io, Heroku
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.
- http://socket.io/docs/server-api/
- https://github.com/Automattic/socket.io-adapter
- 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
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'); //Adaptervar 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));
Following code works for me with Heroku Redis, hope this helps.
var app = express();var server = require('http').Server(app);var io = require('socket.io')(server);var redis = require('redis');var redisAdapter = require('socket.io-redis');io.adapter(redisAdapter({ pubClient: redis.createClient(process.env.REDIS_URL, {return_buffers: true}), subClient: redis.createClient(process.env.REDIS_URL, {return_buffers: true})}));