Redis provides a set of commands that enable developers to implement the Publish/Subscribe mode. Publish/Subscribe enables inter-process messaging, which works like this:
The Publish/Subscribe mode contains two roles, publisher and Subscriber, respectively. Subscribers can subscribe to one or more channels (channel), and publishers can send messages to the specified channel, and all subscribers to the channel receive this message.
The publisher's command to post a message is PUBLISH, using PUBLISH channel message, such as saying "HI" to CHANNEL.1:
redis> PUBLISH channel.1 hi (integer) 0
So the news was sent out. The return value of the PUBLISH command indicates the number of subscribers who received the message. Because there is no client subscription channel.1 at this time, 0 is returned. messages sent out will not be persisted, that is, when a client subscribes to CHANNEL.1, it can only receive subsequent messages that are posted to that channel, which is not received.
Subscription channel command is SUBSCRIBE, can subscribe to multiple channels at the same time, the use is SUBSCRIBE Channel [channel ...]. now open a new REDIS-CLI instance a and use it to subscribe to CHANNEL.1:
Redis a> SUBSCRIBE channel.1reading messages ... (Press Ctrl-c to quit) 1) "Subscribe" 2) "CHANNEL.1" 3) (integer) 1
After executing the SUBSCRIBE command, the client enters the subscription state and in this state the client cannot use the 4 commands other than the SUBSCRIBE, unsubscribe, Psubscribe, and Punsubscribe commands that are part of the Publish/subscribe mode. Otherwise you will get an error.
Clients may receive 3 types of replies after entering the subscription status. Each type of reply contains 3 values, the first value is the type of the message, and the second to third value has a different meaning depending on the message type. There are 3 possible values for message types:
(1) Subscribe. Represents the feedback for a successful subscription. The second value is the channel name for which the subscription succeeds, and the third value is the number of channels subscribed to by the current client.
(2)message. Represents the received message. the second value represents the channel name that generated the message, and the third value is the content of the message.
(3) Unsubscribe. Indicates that a channel was successfully unsubscribed. The second value is the corresponding channel name, the third value is the number of channels subscribed by the current client, and when the secondary value is 0 o'clock the client exits the subscription state and then executes other commands that are not "publish/subscribe" mode.
In the example above, when instance a subscribes to CHANNEL.1 to enter the subscription state and receives a subscribe type of reply, we open another REDIS-CLI instance B and send a message to CHANNEL.1:
Redis b> PUBLISH channel.1 hi! (integer) 1
A return value of 1 indicates that a client has subscribed to CHANNEL.1, and instance a receives a reply of type message:
1) "Message" 2) "CHANNEL.1" 3) "hi!"
Use the unsubscribe command to cancel the specified channel, using unsubscribe [Channel [channel ...]], and unsubscribe to all channels if you do not specify a channel. The unsubscribe command could not be tested in it due to redis-cli restrictions.
Subscribe by rules
In addition to using the SUBSCRIBE command to subscribe to a channel with the specified name, you can subscribe to the specified rule using the Psubscribe command. The rules support the GLOB style wildcard format , and the following opens a new REDIS-CLI instance C for demonstration:
Redis c> psubscribe channel.? *reading messages ... (Press Ctrl-c to quit) 1) "Psubscribe" 2) "channel.? * "3) (integer) 1
Regular channel.? * Can match CHANNEL.1 and channel.10, but will not match channel1. At this point, the message is published in instance B:
127.0.0.1:6379> PUBLISH CHANNEL.1 hi! (integer) 2
The return result is 2 because both instance a and instance C two clients subscribe to the CHANNEL.1 channel. The reply received by instance C is:
1) "Pmessage" 2) "channel.? * "3") "CHANNEL.1" 4) "hi!"
The first value indicates that the message was received by subscribing to the channel through the Psubscribe command, the second value represents the wildcard character used at the time of the subscription, the third value represents the channel command that actually received the message, and the third value is the message content.
You can use the Psubscribe command to subscribe to a channel repeatedly , such as a client executing Psubscribe channel. Channel.? *, when you want to channel.2 the message, the client receives two messages, while the PUBLISH command returns a value of 2 instead of 1. Similarly, if there is another client that performs the SUBSCRIBE Channel 10 and Psubscribe channel.? * If you send a command to channel.10 the client also receives two messages (but two types: message and Pmessage), and the PUBLISH command returns 2.
The Punsubscribe command can unsubscribe from the specified rule, using the punsubscribe [pattern [pattern ...]], which will unsubscribe from all rules if no parameters are available.
The punsubscribe command can only be used to unsubscribe from the rules subscribed by the Psubscribe command, without affecting the channels subscribed directly through the SUBSCRIBE command, nor does the unsubscribe command affect the rules subscribed by the Psubscribe command. Another error is that using the Punsubscribe command to unsubscribe from a rule does not expand the wildcard character, but rather strict string matching , so Punsubscribe * Cannot unsubscribe channel.* rules, but must use Punsubscribe channel.* to unsubscribe.
Redis-Publish/Subscribe mode