Redis publish-subscribe model

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.

insert image description here

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.
insert image description here
Test query subscription:
insert image description here

Usage scenarios: chat rooms, bulletin boards, information decoupling between services, etc.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324157015&siteId=291194637