Redis Research (16)-publishing/subscription mode, redis subscription
In the previous article, we wrote the Redis task queue.
In addition to implementing the task queue, Redis also provides a set of commands for developers to implement"Publish/subscribe" (publish/subscribe) Mode. The "Publish/subscribe" mode can also implement message transmission between processes. The principle is as follows:
The "Publish/subscribe" mode contains two roles: publisher and subscriber. A subscriber can subscribe to one or more channels, and a publisher can send messages to a specified channel. All subscribers that subscribe to this channel will receive this message.
The command used by the publisher to PUBLISH a message is PUBLISH and is used as the PUBLISH channel message. For example, you can say "hi" to channel.1 ":
redis＞PUBLISH channel.1 hi(integer) 0
In this way, the message is sent. The Return Value of the PUBLISH command indicates the number of subscribers that receive the message. Because no client subscribes to channel.1 at this time, 0 is returned. Messages sent are not persistent. That is to say, when a client subscribes to channel.1, it can only receive messages subsequently published to this channel, but those sent earlier will not be received.
The SUBSCRIBE channel command can SUBSCRIBE to multiple channels at the same time. The usage is SUBSCRIBE channel [channel…]. Now we 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 the SUBSCRIBE command is executed, the client enters the subscription status, in this status, the client cannot use commands other than SUBSCRIBE, UNSUBSCRIBE, PSUBSCRIBE, and PUNSUBSCRIBE commands in the "Publish/SUBSCRIBE" mode (the following three commands will be described below), otherwise, an error is reported.
After entering the subscription status, the client may receive three types of replies. Each type of reply contains three values. The first value is the message type. The second and third values have different meanings depending on the message type. Possible values of message types include:
(1) Subscribe. Feedback message indicating successful subscription. The second value is the name of the channel subscribed successfully, and the third value is the number of channels subscribed by the current client.
(2) message. This type of reply is what we are most concerned about. It indicates the received message. The second value indicates the channel name of the message, and the third value is the message content.
(3) unsubscribe. Indicates that the subscription to a channel is successfully canceled. The second value is the corresponding channel name, and the third value is the number of channels subscribed by the current client. When this value is 0, the client will exit the subscription status, then you can execute other commands that are not in the "Publish/subscribe" mode.
In the above example, when instance A subscribes to channel.1 and enters the subscription status, it receives A reply of the subscribe type. In this case, we open another redis-cli instance B and send A message to channel.1:
redis B＞PUBLISH channel.1 hi!(integer) 1
If the returned value is 1, A client subscribes to channel.1. At this time, instance A receives A message-type reply:
1) "message "2) "channel.1"3) "hi!"
You can use the UNSUBSCRIBE command to UNSUBSCRIBE to a specified channel in the format of UNSUBSCRIBE [channel [channel…]. If no channel is specified, the subscription will be canceled.
Subscription by rule
In addition to the SUBSCRIBE command, you can also use the PSUBSCRIBE command to SUBSCRIBE to the specified rule. The rule supports the glob-style wildcard format. Here we will open 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
Rule channel .? * It can match channel.1 and channel.10, but it does not match channel .. In this case, the message is published in instance B:
redis B＞PUBLISH channel.1 hi!(integer) 2
The returned result is 2 because both client A and client C have subscribed to channel channel.1. 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 through the PSUBSCRIBE command subscription channel, the second value indicates the wildcard used for subscription, and the third value indicates the channel Command actually received the message, the fourth value is the message content.
You can use the PSUBSCRIBE command to subscribe to a channel again. For example, a client executes the PSUBSCRIBE channel .? Channel .? * After a message is published to channel.2, the client receives two messages, and the value returned by the PUBLISH command is 2 instead of 1. Similarly, if another client executes SUBSCRIBE channel.10 and PSUBSCRIBE channel .? * If you send a command to channel.10, the client will also receive two messages (but two types, message and pmessage), and the PUBLISH command will return 2.
The PUNSUBSCRIBE command can unsubscribe the specified rule. The usage is PUNSUBSCRIBE [pattern [pattern…], If no parameter exists, all rules are unsubscribed.
The PUNSUBSCRIBE command can only UNSUBSCRIBE rules subscribed by the PSUBSCRIBE command, without affecting the channels directly subscribed by the SUBSCRIBE command. Similarly, the UNSUBSCRIBE command does not affect the rules subscribed by the PSUBSCRIBE command. Another error is that the PUNSUBSCRIBE command does not expand the wildcard characters when unsubscribing to a rule, but strictly matches the strings. Therefore, PUNSUBSCRIBE * cannot Unsubscribe the channel. * rules, but must use PUNSUBSCRIBE channel. * Can Be unsubscribed.