I. Overview
- Redis publish and subscribe (pub / sub) messaging is a communication mode: a sender (Pub) sends a message, the subscriber (Sub) receives the message.
- Redis clients can subscribe to any number of channels.
- The figure below shows the channel channel1, and three clients subscribe to this channel - the relationship between client2, client5 and client1:
When a new message is sent to the channel by channel1 PUBLISH command, this message will be sent to subscribers of its three clients:
Second, the actual presentation
- In our example, we created a subscription channel called redisChat:
Subscriber client:
redis 127.0.0.1:6379> SUBSCRIBE redisChat
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "redisChat"
3) (integer) 1
- Now, we have to re-open a redis client , then redisChat published twice in the same news channel, subscribers will be able to receive the message.
Posted by clients:
redis 127.0.0.1:6379> PUBLISH redisChat "Redis is a great caching technique"
(integer) 1
redis 127.0.0.1:6379> PUBLISH redisChat "Learn redis by runoob.com"
(integer) 1
Subscriber client:
# 订阅者的客户端会显示如下消息
1) "message"
2) "redisChat"
3) "Redis is a great caching technique"
1) "message"
2) "redisChat"
3) "Learn redis by runoob.com"