Publish/Subscribe (pub/sub) for redis

         Publish and subscribe (pub/sub) is a message communication mode whose main purpose is to decouple the coupling between message publishers and message subscribers.

         As a pub/sub server, Redis functions as message routing between subscribers and publishers.

         Subscribers can subscribe to the redis server for message types they are interested in through the subscribe and psubscribe commands. Redis calls the message type a channel.

         When the publisher sends a specific type of message to the redis server through the publish command, all clients that subscribe to the message type will receive the message. The message delivery here is many-to-many. A client can subscribe to multiple channels, and can also subscribe to multiple channels. Multiple channels send messages.

 

1. Subscribe and Publish:

To subscribe to foo and bar, a client issues a subscribe channelname operation:

    SUBSCRIBE foo bar

At this point, from another client we issue a publish operation on the channel name second:

    PUBLISH foo  Hello

At this point the first client will receive messages from the channel it subscribed to:

1) "message"

2) "foo"

3) "haha Hello"

 

2. Pattern matching subscription

Subscription operations can also be performed through pattern matching:

PSUBSCRIBE  foo news.*

(Means to subscribe to all channels starting with news., for example: news.art.figurative, news.music.jazz)

 

3. Unsubscribe

UNSUBSCRIBE and PUNSUBSCRIBE (pattern matching)

 

 

Guess you like

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