I'm curious about how scale out WebSocket-based implementation of, for example, chat-like service. Can it be easily scaled out to multiple app servers? I think Server-Sent Events and Redis can easily solve the problem.
- The biggest difference between WebSocket and SSE is bi-directional or not.
- Posting messages can be done via usual XHR.
- So, the problem just sits on how server can sent message chunks to clients in real time.
Pub/Sub with replication by Redis
- Redis installed via homebrew
$ redis-server -v Redis server v=2.6.4 sha=00000000:0 malloc=libc bits=64
Add the settings below into config file for slave (name the file
slave.conf copied from
port 6380 slaveof 127.0.0.1 6379
Launch Redis servers
$ redis-server # master
$ redis-server slave.conf # slave
$ redis-cli -p 6379 redis 127.0.0.1:6379>
$ redis-cli -p 6379 redis 127.0.0.1:6379> SUBSCRIBE 'test' Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "test" 3) (integer) 1
$ redis-cli -p 6380 redis 127.0.0.1:6379> SUBSCRIBE 'test' Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "test" 3) (integer) 1
From the publisher console:
redis 127.0.0.1:6379> PUBLISH 'test' booooo (integer) 1
Then both of clients get message from publisher!
The client connecting to the master:
redis 127.0.0.1:6379> SUBSCRIBE 'test' ... 1) "message" 2) "test" 3) "booooo"
The client connecting to the slave:
redis 127.0.0.1:6380> SUBSCRIBE 'test' ... 1) "message" 2) "test" 3) "booooo"
Use anything you like to send message to all the clients connecting to server via SSE.
Here's my PoC implementation using Rails4. It now seems not to work well due to the change of Rails4, though.
- Easy to scale out bottle necks.
- SSE is more understandable than WebSockets
- We need one more middleware (Redis)
- Any other?
- Replication setting of Redis is easy.
- Pub/Sub in Redis works fine also in replication.
- In the sense of ease of scaling out, SSE + Pub/Sub with Redis can be better for chat-like service than WebSocket with single event-besed server.
What do you think?