How ActionCable uses Redis

Published Jan 21, 2017Last updated Feb 22, 2017
How ActionCable uses Redis

So PubSub is this concept that Redis and Postgres and a few other things supports, and basically you have this ability to have Channels and the ability to subscribe (and receive those messages) and publish to those channels and anyone listening will then receive those messages.

An example of this is a ChatroomChannel, you could have people publishing messages to the ChatroomChannel and anyone who was subscribed to the ChatroomChannel would receive the broadcast from the channel with the websocket.

Redis CLI

You can hop into the Redis CLI assuming you have redis installed already with


Now we can subscribe to a "chat" channel with

subscribe "chat"

You should see something like

Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "chat"
3) (integer) 1

Now if you open up another two more terminal windows, one running the redis-cli and running subscribe "chat" like we did above and then in the second terminal window we run

redis-cli and then run publish "chat" "hello!"

Now if you notice, you'll receive an integer back. This integer represents the number of subscribers received the message from Redis.

Both of our subscribed Redis tabs received the message of Hello.


So let's hop back into those subscribed terminals we opened and clear it out and go back into the redis-cli and run subscribe "notifications:1" on both windows... and then we hop into the Rails app we made with the Realtime Notifications with ActionCable Tutorial

And then we jump into the rails console inside of the app and run

ActionCable.server.broadcast "notifications:1", {html: "<div>Hello World!</div>"}

Usually you'll send over JSON with the ActionCable.server.broadcast so that's what we do above. Now if you run that and go back to your Redis terminals you'll see you received a message on notifications:1 and you received the escaped HTML inside of the JSON.

If you had a tab open on the Notifications page, you'd also have received it there as well.

Credits for GoRails

This transcript was written by Andrew Fomera.

Discover and read more posts from Victor H
get started
Enjoy this post?

Leave a like and comment for Victor