How to implement Redis PubSub in Websocket channels.

814 views
Skip to first unread message

Isaac Ben Hutta

unread,
Oct 2, 2017, 10:50:11 PM10/2/17
to Redis DB

I'm trying to figure out how to implement in Redis websocket channels.

Where I get confused is with PubSub.

Let's assume my app has two channels that users can subscribe too - "lobby", "room:*". The room channel has a wildcard so it can be any channel that start with "room:".
How does this work in Redis? If a user subscribed to "room:history" how would Redis know to publish to this sub-channel only? 
Do I need to subscribe to these channels on init per server? Or the subscription is per user? 

Mani Gandham

unread,
Oct 3, 2017, 9:08:37 AM10/3/17
to Redis DB

Pub/sub is a generic messaging model. Subscribers express interest in a certain topic and receive all messages that match that topic, whether it's a wildcard pattern or an exact topic name.

You can have your app subscribe to the room:* topic and receive all messages for all rooms or you can have it subscribe individually to each separate room like room:abc and room:xyz depending on what you need. You can read about the technical workings of pubsub in Redis in this article: https://making.pusher.com/redis-pubsub-under-the-hood/

In your case, it seems like you want users connected to certain rooms over websockets? Redis doesn't support websockets so you need a server app in the middle. Your app can connect to Redis with a wildcard subscription to room:* and then send the appropriate messages to users over websockets depending on what specific room they're subscribed to, but that logic would have to be in your app. When a user subscribes to a room, you can store that in Redis and then every time your app receives a message, lookup the topic name, see if any users are subscribed, and if there are, send them the message over websockets. This is a simple approach that won't scale infinitely but works well to get going.
Reply all
Reply to author
Forward
0 new messages