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.
Using Redis (Pub/Sub and replication), can we get more robust architecture?
Pub/Sub with replication by Redis
Redis settings
- Redis installed via homebrew
$ redis-server -v Redis server v=2.6.4 sha=00000000:0 malloc=libc bits=64
Slave setting
Add the settings below into config file for slave (name the file slave.conf
copied from redis.conf
):
port 6380 slaveof 127.0.0.1 6379
Launch Redis servers
master:
$ redis-server # master
slave:
$ redis-server slave.conf # slave
Launch clients
publisher:
$ redis-cli -p 6379 redis 127.0.0.1:6379>
subscribers:
$ 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
Publish something:
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"
App server
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.
http://blog.kentarok.org/entry/2012/11/22/005844
Pros.
- Easy to scale out bottle necks.
- SSE is more understandable than WebSockets
Cons.
- We need one more middleware (Redis)
- Any other?
Conclusion
- 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?
2 comments
This is exactly what I am currently working on. The SSE literally makes life really easy and the code is really simple on the frontend too. However, I code in PHP and I have implemented a notification server using PHP, NODE, REDIS and SOCKET.io combined. I have yet to see a working demo of using SSE and REDIS together. Maybe I’ll sit out and code it today or tomorrow. A working demo is what I felt was lacking in your article.
The code I wrote before that proves my concept is here
http://blog.kentarok.org/entry/2012/11/22/005844
However, it doesn’t seem to work now due to change of Rails4…