2

I'm confused about running node with more than one dyno on Heroku and how to make socket.io/redis accessible across instances. I'm using socket.io 0.9.16.

(1) First, when I scale to two dynos in Heroku, this just means two node instances?

(2) Second, When I create an instance of io like so:

var io = require('socket.io').listen(server); 

then when a second node instance gets created then a second io instance gets created with it - is that correct?

(3) And the reason to use redis is to create a single store accessible by all instances - is that correct?

However, when I create a new redisstore for io like so:

var ioRedisStore = require('socket.io/lib/stores/redis');
io.set('store', new ioRedisStore({
  redisPub    : redis.createClient(),
  redisSub    : redis.createClient(),
  redisClient : redis.createClient()
}));

(4) Isn't this redis instance now created by each node instance and then set as the store for each io instance? How does this cross instances?

1 Answer 1

4

(1) First, when I scale to two dynos in Heroku, this just means two node instances?

You can think of it that way, yes.

(2) Second, When I create an instance of io like so: ...

Yes, that is correct.

(3) And the reason to use redis is to create a single store accessible by all instances - is that correct?

Yes.

(4) Isn't this redis instance now created by each node instance and then set as the store for each io instance? How does this cross instances?

Redis is a key-value server. Calling redis.createClient() just creates a connection to a redis server, it doesn't actually create an instance of a redis server anywhere. You're going to need to have an instance of redis running somewhere (Heroku has a few options). Then you are going to need to setup your code to explicitly connect to that one instance by passing the port and host to redis.createClient (docs).

So, if you have a redis instance running on port 6379 (default) at 10.0.1.100 (picked randomly), you'd do:

var ioRedisStore = require('socket.io/lib/stores/redis');
io.set('store', new ioRedisStore({
  redisPub    : redis.createClient(6379, '10.0.1.100'),
  redisSub    : redis.createClient(6379, '10.0.1.100'),
  redisClient : redis.createClient(6379, '10.0.1.100')
}));

That way all your node instances share the same redis instance and they can coordinate through it.

Sign up to request clarification or add additional context in comments.

1 Comment

That explanation for (4) was exactly what I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.