I'm developing a site with Node.js which has many realtime features. One feature is, that users can publish something in a certain channel and this will be immediately pushed to everyone watching the same channel.
I am trying to figure out the best way to approach this. The data structure of MongoDB fits my needs very well, but Redis Pub/Sub feature seems extremely fitting for this problem.
So I thought I might store the full dataset in MongoDB and just add a reference in Redis to push it to the neccessary channels. Then the subscriber clients of these channels could read the full data from the MongoDB database.
Does this make sense or am I missing something?
Thanks!
It makes sense. You can use Redis natively or through the socket.io API, and use MongoDB for persistent data.
The only gotcha is to realize that Redis pub/sub does not provide any queuing facility. Redis does not buffer the notifications, they go straight from the publisher socket to the subscriber sockets, in the same event loop iteration. This is fast, but if a subscriber closes its connection, it may lose some items before the connection is established again. The delivery is not guaranteed at all.
However, a subscriber reconnecting after a disconnection could use the MongoDB data to get the items it has missed if required.