Redis Pub Sub
# Redis Publish/Subscribe
Redis Publish/Subscribe (pub/sub) is a messaging pattern where publishers (pub) send messages and subscribers (sub) receive them.
Redis clients can subscribe to any number of channels.
The figure below illustrates the relationship among channel `channel1` and three clients subscribed to it β `client2`, `client5`, and `client1`:
!(#)
When a new message is sent to channel `channel1` via the `PUBLISH` command, the message is delivered to all three subscribers:
!(#)
* * *
## Example
The following example demonstrates how publish/subscribe works. You need to start two `redis-cli` clients.
In our example, we create a subscription channel named **tutorialChat**:
## First redis-cli client
redis 127.0.0.1:6379> SUBSCRIBE tutorialChat
Reading messages... (press Ctrl-C to quit)
1)"subscribe"
2)"tutorialChat"
3)(integer)1
Now, let's open another redis client and publish two messages to the same channel `tutorialChat`; the subscriber will receive both messages.
## Second redis-cli client
redis 127.0.0.1:6379> PUBLISH tutorialChat "Redis PUBLISH test"
(integer) 1
redis 127.0.0.1:6379> PUBLISH tutorialChat "Learn redis by .com"
(integer) 1
# The subscriber's client will display the following messages
1) "message"
2) "tutorialChat"
3) "Redis PUBLISH test"
1) "message"
2) "tutorialChat"
3) "Learn redis by .com"
GIF demonstration as follows:
* Start your local Redis server and launch two `redis-cli` clients.
* In the **first redis-cli client**, enter `SUBSCRIBE tutorialChat`, meaning to subscribe to the `tutorialChat` channel.
* In the **second redis-cli**
YouTip