Cade Embery
Cade Embery

Reputation: 450

Horizontally scaling socket.io across multiple Amazon EC2 instances

Have everything setup and working correctly with 2 Amazon EC2 servers running my node application. Currently sitting behind an Application Load Balancer.

I am emitting socket messages using code like this

const ioredis = require('socket.io-redis');                
io = require('socket.io')(server);

io.adapter(ioredis({
    host: 'localhost',
    port: 6379
}));


io.to('roomName').emit('message', data);

However, when emitting a message using socket.io, the message is only broadcast from the originating node server.

Server 1 emits a socket.io message -> Anyone connected to Server 1 hears the message, but anyone connected to Server 2 doesn't.

Server 2 emits a socket.io message -> Anyone connected to Server 2 hears the message, but anyone connected to Server 1 doesn't.

What i need help with is how to emit the socket.io message from Server 1 and either Relay it to all other server's aswell, possibly by using a single instance of redis on a seperate server?

OR

Have another Server as a socket emitter that Server 1 and Server 2 send and receive socket messages from.

Not quite sure which option is the best and correct way to go, any help would be appreciated.

Upvotes: 4

Views: 1425

Answers (1)

Cade Embery
Cade Embery

Reputation: 450

I managed to solve the problem by creating a 3rd server, installing Redis on it and setting all of the other servers to use that single instance of redis.

Now socket messages are broadcast to all servers regardless of the originating server.

Upvotes: 6

Related Questions