Location>code7788 >text

Redis Publish Subscription Model

Popularity:547 ℃/2024-10-03 09:39:07

summarize

Redis publish/subscribe is a message communication model: a sender (Pub) sends messages to a channel and a subscriber (Sub) receives messages on the channel.A Redis client can subscribe to any number of channels and a sender can send data to any channel. After a sender sends a message to a channel, that message is sent to the client (Sub) that is subscribed to that channel

Redis has two publish/subscribe modes:

  • Channel-based publishing/subscribing
  • Pattern-based Publish/Subscribe

The following are common Redis publish-subscribe mode commands:

  • subscribe channel [channel ...] : Subscribe to the given channel or channels
  • unsubscribe channel [channel ...]: unsubscribe the given channel. : Unsubscribe from the given channel, if no channel is specified, all channels are unsubscribed by default.
  • publish channel message: send the message to the specified channel channel, return the number of subscribers who received the message, no subscriber returns 0.
  • pubsub channels [argument [atgument ...]] : view the status of the subscription and publishing system, returning a list of active channels (i.e. channels with at least one subscriber)
  • psubscribe pattern1 [pattern...] : Subscribes to one or more channels that match a given pattern, with * as a match for each pattern, e.g.cn* Matches all channels starting with cn:
  • punsubscribe [pattern [pattern ...]] : Unsubscribe from all channels with the given pattern. pattern is unspecified, all patterns subscribed to are unsubscribed