Publish and subscribe introduction

Publish and subscribe is a message communication pattern between processes in which the sender (PUB) sends the message and the subscriber (sub) receives the message. Subscribe Only the channel that subscribes to the specified sender can receive the message pushed by the specified sender, similar to wechat users who subscribe to the specified subscription number can receive the text message pushed by the specified subscription number.

The Redis client subscribes to channel channel1 with the subscribe command:The sender publishes the message to channel channel1 and pushes it to the subscribing client:

The command

The command describe
PUBLISH channel message Sends a message to the specified channel
SUBSCRIBE channel [channel …] Subscribe to one or more channels of messages
PSUBSCRIBE pattern [pattern …] Subscribe to one or more channels that fit a given pattern
UNSUBSCRIBE [channel [channel …]] Unsubscribe from one or more channels
PUNSUBSCRIBE [pattern [pattern …]] Unsubscribe from one or more channels that conform to a given pattern
PUBSUB subcommand [argument [argument …]] View subscription and publishing system status

In field

Subscribers subscribe to channels C1, C2 and C3The sender sends a1, A2 and A3 messages to C1, C2 and C3, respectivelySubscribers can receive messages pushed by C1, C2 and C3 respectivelySubscribers subscribe to all channels starting with C and A by wildcardThe sender sends hellokitty and helloworld messages to cc and aa channels, respectivelySubscribers subscribe to channels that match CC and AA channels, so they can receive messages pushed by those two channels

conclusion

  1. Subscribers subscribe to channels before receiving messages from senders
  2. Subscribed channels are case sensitive

In practice, publishing and subscribing messages are not usually handled by Redis, so this chapter is a brief description