What is Redis pubsub and how do I use it?

24,136

Solution 1

Publish/subscribe is a pretty simple paradigm. Think of it like you're running a talk show on a radio station. That's PUBLISH. You're hoping at least one or more people will pick up your channel to listen to your messages on the radio show (SUBSCRIBE) and maybe even do some stuff, but you're not talking to folks directly.

Let's have some fun with redis-cli!

redis 127.0.0.1:6379> PUBLISH myradioshow "Good morning everyone!"
(integer) 0
redis 127.0.0.1:6379> PUBLISH myradioshow "How ya'll doin tonight?"
(integer) 0
redis 127.0.0.1:6379> PUBLISH myradioshow "Hello? Is anyone listening? I'm not wearing pants."
(integer) 0

Notice there are no clients receiving the messages on your "myradioshow" channel (that's the 0 in the response). Nobody is listening. Now, open another redis-cli (or for more fun times have a friend open up their redis-cli and connect to your server) and SUBSCRIBE to the channel:

redis 127.0.0.1:6379> SUBSCRIBE myradioshow
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "myradioshow"
3) (integer) 1

Go back to your original redis-cli and continue your show:

redis 127.0.0.1:6379> PUBLISH myradioshow "Next caller gets a free loaf of bread!"
(integer) 1

Notice that "1" at the end? You have a listener! Like magic, in your SUBSCRIBE-d terminal:

1) "message"
2) "myradioshow"
3) "Next caller gets a free loaf of bread!"

Of course, in reality, you're probably going to want to do stuff that's more useful than telling your clients about your pants-less lifestyle, such as firing events on your server or running some kind of tasks/jobs. Maybe not though! :)

Solution 2

Supplementary to the answer that was provided by Aashay Desai I want to state that I landed here, searching for that exact part:

how to create a channel

I kept myself asking where to get the name of the channel I can use to PUBLISH from. There is no source stating, that the channel you are referring to is the one that was used in the PUBLISH-statement.

To be clear: by using the PUBLISH statement, you are creating the channel.

To keep the example from above:

PUBLISH myradioshow "Next caller gets a free loaf of bread!"

You publish to the channel "myradioshow" that you are also creating in this exact moment. It is totally up to you how to name it.

For a bit more experienced developer this maybe is a self-explaining thing but from a beginner's point of view, even 10 years after this question being answered here, I did not stumble across one single source/documentation that explicitly states this.

Share:
24,136

Related videos on Youtube

Alfred
Author by

Alfred

Updated on April 12, 2022

Comments

  • Alfred
    Alfred about 2 years

    Somebody asked me what PubSub was and how to create a channel (in comment from my answer) and I pointed him to the article on redis.io => http://redis.io/topics/pubsub. I think it is pretty clear, but I am wondering if somebody has a better explanation. Ideally, describe it clearly using redis-cli.

  • bcoughlan
    bcoughlan over 11 years
    Why does the subscriber get "subscribe" and "myradioshow" as messages instead of "Good morning everyone!" etc.?
  • Aashay Desai
    Aashay Desai over 11 years
    Those are just acknowledgement messages back from Redis in the form of a bulk response, telling you what it did and what it subscribed to, and the 1 indicates a success response. See here: redis.io/commands/subscribe. In my example, the SUBSCRIBE happens after the initial messages got published, so they don't receive "Good morning everyone!" etc because those messages are already gone. In other words, SUBSCRIBE only receives published messages that are published after the subscription (so in other words, it's not a queue).
  • kentor
    kentor almost 7 years
    @AashayDesai a common scenario when one wants/needs to use publish & subscribe would be helpful
  • vampiire
    vampiire about 2 years
    jeeze i feel the same way man. the other thing that is unclear is the difference between subscribe and psubscribe, or how to discover what is available (channels / patterns etc). i am able to psubscribe to a pattern yet when i list channels it says 0.
  • Kamegohan
    Kamegohan about 2 years
    @vampiire unfortunately, I am not into that topic very deeply. Looking into the documentation I think 'PUBSUB CHANNELS' will not work and 'PUBSUB NUMPAT' could be the answer you are searching for. But that's just a wild guess and I hope you will find an appropriate solution