Pub/Sub Introduction

Apply for Redis Developer Course Redis Regular Inspection / Technical Support Redis Enterprise Server

Introduction

Sending and receiving messages

Unlike a typical database, Redis provides a way to send and receive messages.   You send with PUBLISH and receive with SUBSCRIBE.
The channel is used as the path.   Think of a channel as similar to the 'KEY' used in "SET KEY VALUE".
The method is: on client 1 run subscribe channel_name, on client 2 run publish channel_name "Message", and "Message" appears on client 1.
The Redis Pub/Sub system does not queue messages.   They are delivered only to clients that are already waiting with a subscribe command at the moment of publish.

Now let's look at the commands one by one.

Command descriptions

SUBSCRIBE channel [channel ...]

Receives messages sent to the specified channel.   You can specify multiple channels.

    6379> subscribe ch01 ch02
    Reading messages... (press Ctrl-C to quit)
    1) "subscribe"
    2) "ch01"
    3) (integer) 1
    1) "subscribe"
    2) "ch02"
    3) (integer) 2

If another client sends a message with PUBLISH ch01 "Hello!", it is received as follows.

    1) "message"
    2) "ch01"
    3) "Hello!"

PSUBSCRIBE pattern [pattern ...]

Register channels by pattern. You can register multiple patterns.
Patterns support glob-style matching as follows.

    • '?' matches one character.   h?llo means hello, hallo, hxllo, and so on.
    • '*' matches zero or more characters.   h*llo means hllo, heeeello, and so on.
    • h[ae]llo allows only 'a' or 'e'.   So hello and hallo match, but hillo does not.

Example of registering a pattern

    6379> psubscribe ch*
    Reading messages... (press Ctrl-C to quit)
    1) "psubscribe"
    2) "ch*"
    3) (integer) 1

If another client sends a message with PUBLISH ch01 "Hello! AlphaGo", it is received as follows.

    1) "pmessage"
    2) "ch*"
    3) "ch01"
    4) "Hello! AlphaGo"

UNSUBSCRIBE [channel [channel ...]]

Removes channels registered with SUBSCRIBE so messages are no longer received.
If you omit the channel name, all channels registered for that client are removed.
If you specify names, only those channels are removed.
In redis-cli, exit with Ctrl-C.

    6379> unsubscribe ch*
    1) "unsubscribe"
    2) "ch*"
    3) (integer) 1

PUNSUBSCRIBE [pattern [pattern ...]]

Removes patterns registered with PSUBSCRIBE so messages are no longer received.
If you omit the pattern name, all patterns registered for that client are removed.
If you specify names, only those patterns are removed.
In redis-cli, exit with Ctrl-C.

    6379> punsubscribe ch*
    1) "punsubscribe"
    2) "ch*"
    3) (integer) 1

PUBLISH channel message

Sends a message to the specified channel.
Returns the number of clients that received the message.

    6379> publish ch01 "Hello! AlphaGo"
    (integer) 1

PUBSUB subcommand [argument [argument ...]]

The PUBSUB command looks up channels or patterns registered on the server.
There are three subcommands:   channels, numsub, and numpat.
channels and numsub are related to channels, and numpat is related to patterns.

PUBSUB channels [pattern]

If you omit Pattern, all channel names registered on that server are shown.
Here pattern is not a pattern registered with psubscribe; it is used to find channel names in glob-style.
It does not show registered patterns.
Even if clients register the same channel name multiple times, this command shows it only once.

    6379> pubsub channels
    1) "ch01"
    2) "ch02"

PUBSUB numsub [channel-1 ... channel-n]

Shows the number of clients registered on that channel.
For each channel found with PUBSUB channels, it shows how many clients are connected.

    6379> pubsub numsub ch01 ch02
    1) "ch01"
    2) (integer) 3
    3) "ch02"
    4) (integer) 1

PUBSUB numpat

Shows the number of patterns registered on the server.

    6379> pubsub numpat
    (integer) 3

Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

<< BITPOS Pub/Sub Intro Redis Config & Pub/Sub >>

Email 답글이 올라오면 이메일로 알려드리겠습니다.