Redis message communication mode: publish and subscribe

Redis publish and subscribe (pub/sub) is a message communication mode: the sender (pub) sends the message, and the subscriber (sub) receives the message.

Redis clients can subscribe to any number of channels.

The following figure shows the relationship between channel channel1 and the three clients that subscribe to this channel-client2, client5 and client1:

 

When a new message is sent to channel channel1 through the PUBLISH command, the message will be sent to the three clients that subscribe to it:

Open two redis-cli, one to create a subscription, and one to send a message to the created subscription:

But if you publish data to a non-existent subscription, it will return 0

The following table lists the commonly used redis publish and subscribe commands:

Serial number Command and description
1 PSUBSCRIBE pattern [pattern ...]
Subscribe to one or more channels that conform to a given pattern.
2 PUBSUB subcommand [argument [argument ...]]
View the status of the subscription and publishing system.
3 PUBLISH channel message
sends information to the specified channel.
4 PUNSUBSCRIBE [pattern [pattern ...]]
Unsubscribe from all channels of a given pattern.
5 SUBSCRIBE channel [channel ...]
Subscribe to the information of a given channel or channels.
6 UNSUBSCRIBE [channel [channel ...]]
means to unsubscribe from a given channel.

 

Guess you like

Origin blog.csdn.net/weixin_40179091/article/details/114893660