In the previous article we wrote the Redis task queue.
In addition to implementing the task queue, Redis also provides a set of commands that enable developers to implement the Publish/subscribe (publish/subscribe) mode . The "Publish/subscribe" mode also enables interprocess messaging, which is the principle:
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><span style= "FONT-SIZE:14PX;" >publish channel</span>.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, in which case the client cannot use except for the Subscribe/unsubscribe/psubscribe/punsubscribe 4 of the "Publish/Subscribe" Commands outside of the mode (the following 3 commands are described below), otherwise an error will be presented.
clients may receive three 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. Possible values for message types are:
(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. This type of reply is our top concern and it 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 this value is 0 o'clock the client exits the subscription state and then executes other commands that are not in the 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 unsubscribe to the specified channel, using Unsubscribe[channel [Channel ...]], and unsubscribe from all channels if you do not specify a channel.
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 . Rules support GLOB style wildcard format, below we 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
Regular channel.? * Can matchChannel.1 andChannel.10, but does not matchChannel.。 At this point, the message is published in instance B:
Redis b>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 fourth value is the message content.
You can use the Psubscribe command to subscribe to a channel repeatedly , such as a client performing a psubscribe channel. Channel.? *, when a message is posted to CHANNEL.2, the client receives two messages, while the Publish command returns a value of 2 instead of 1. Similarly, if there is another client that executes the 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 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, Instead, you must use Punsubscribe channel.* to unsubscribe .
Redis Research (16)-Publish/Subscribe mode