Lesson 11 / 18

Publish / Subscribe

Broadcast messages to any client listening in real time.

Channels, not keys

Pub/Sub is separate from the key-value store. Clients subscribe to named channels; any client that publishes to that channel fans the message out to all of them instantly.

Subscribing

Open a terminal and subscribe — this client blocks, waiting for messages on the channel.

redis-cli SUBSCRIBE notifications

Output:

1) "subscribe"
2) "notifications"
3) (integer) 1

Publishing

From a second terminal, publish a message — the subscriber above receives it immediately.

redis-cli PUBLISH notifications "New order #4521"

Output:

(integer) 1