Redis publish-subscribe model
Redis provides a message mechanism based on the "publish/subscribe" model. In this model, message publishers and subscribers do not communicate directly. The publisher publishes messages to the specified channel, and the clients subscribed to the channel are all can receive the message.
Several commands are provided in Redis to support the publish-subscribe function:
-
Publish a message to the specified channel: publish channel message
-
Subscribe to messages from one or more channels: subscribe channel [channel …]
-
Unsubscribe from the specified channel: unsubscribe [channel [channel …]]
-
Subscribe to channels by pattern: psubscribe pattern [pattern …]
-
Unsubscribe from a channel by pattern: punsubscribe [pattern [pattern …]]
-
View subscribed channels:
View active channels: pubsub channels [pattern]
View channel subscription count: pubsub numsub [channel ...]
View pattern subscription count: pubsub numpat
Test publish and subscribe:
The client enters the subscription state after executing the subscription command, and can only accept four commands: subscribe, psubscribe, unsubscribe, and punsubscribe. Newly opened subscribed clients cannot receive previous messages from the channel.
Test query subscription:
Usage scenarios: chat rooms, bulletin boards, information decoupling between services, etc.