Redis Publications and subscriptions (pub/sub)
This document is translated from: Http://redis.io/topics/pubsub.
The SUBSCRIBE, unsubscribe, and PUBLISH three commands implement the publish and subscribe information generics (publish/subscribe messaging paradigm), in this implementation, Instead of sending the information directly to a specific recipient (the client receiving the information), the sender sends the information to the channel (channel) and forwards the information to all subscribers interested in the channel.
The sender does not need to know any information about the Subscriber, and the Subscriber does not need to know that the client sends it a message, as long as it focuses on the channel of interest.
Deconstruction of Publishers and Subscribers (decoupling) can greatly improve system extensibility (scalability) and a more dynamic network topology (network topology).
For example, to subscribe to channel Foo and bar, the client can use the channel name as a parameter to invoke the SUBSCRIBE command:
redis> SUBSCRIBE Foo Bar
When a client sends a message to these channels, Redis pushes the incoming information to all the clients subscribing to those channels.
Clients that are subscribing to a channel should not send commands other than SUBSCRIBE and unsubscribe. Where SUBSCRIBE can be used to subscribe to more channels, and unsubscribe can be used to unsubscribe one or more channels that have been subscribed to.
The execution results of SUBSCRIBE and unsubscribe are returned as information, and the client can determine whether the received content is a true message or the operation result of the SUBSCRIBE or unsubscribe command by analyzing the first element of the received information. Format of information
Each piece of information forwarded by the channel is a multiple batch reply (Multi-bulk Reply) with three elements.
The first element of the information identifies the type of information: Subscribe: Indicates that the current client successfully subscribed to the channel indicated by the second element of the information. The third element of the information records the total number of channels currently subscribed to by the client. Unsubscribe: Indicates that the current client successfully withdrew the channel indicated by the second element of the information. The third element of the information records the number of channels the client is still subscribing to. When the number of channels subscribed to by the client drops to 0 o'clock, the client no longer subscribes to any channels, and it can execute any redis command as usual. Message: Indicates that this information is sent by a client executing the PUBLISH command, the real information. The second element of the information is the channel of the information source, and the third element is the content of the information.
For example, if the client executes the following command:
Redis> SUBSCRIBE-Second
Then it will receive the following reply:
1 "Subscribe"
2) "a"
3) (integer) 1
1) "Subscribe"
2) "second"
3) (integer) 2
If at this point, another client executes the following PUBLISH command:
Redis> PUBLISH Second Hello
Then the client who subscribed to the second channel will receive the following information:
1) "Message"
2 "second"
3 "Hello"
When a subscriber decides to unsubscribe from all channels, it can execute a unsubscribe command without parameters:
Redis> Unsubscribe
This command will receive the following reply:
1 "unsubscribe"
2) "second"
3) (integer) 1
1) "Unsubscribe"
2) "" A "(3)"
(integer) 0
Subscription mode
Redis's release and subscription implementations support pattern matching (patterns matching): Clients can subscribe to a pattern with a * number, and if the name of a certain channel matches the pattern, the client receives this/channel information when it is sent to this/channel.
For example, to execute a command
Redis> Psubscribe news.*
Clients will receive information from channels such as News.art.figurative, News.music.jazz, and so on.
The client subscription mode can contain multiple glob-style wildcard characters, such as *,? And [...], and so on.
Execute command
Redis> Punsubscribe news.*
The news.* mode will be unsubscribe and other subscribed modes will not be affected.
The information received through the subscription mode, and the information received through the subscription channel, is not the same: the type of information received through subscription mode is pmessage: This means that a client sends information to a channel through PUBLISH, This channel exactly matches a pattern that the current client subscribes to. The second element of the information records the pattern that was matched, the third element records the name of the matched channel, and the last element records the actual content of the information.
The way clients handle Psubscribe and Punsubscribe return values is similar to how the client handles SUBSCRIBE and unsubscribe: By analyzing the first element of the information, the client can determine whether the information received is a real message or P The return value of the SUBSCRIBE or Punsubscribe command. Receive the same message through channels and modes
If multiple patterns of a client subscription match the same channel, or if the client subscribes to a channel at the same time, and a pattern matches the channel, it may receive the same message multiple times.
For example, if the client executes the following command:
SUBSCRIBE foo
psubscribe f*
Then, when information is sent to channel Foo, the client receives two messages: one from channel foo, the message type, and the other from the schema f*, with the information type Pmessage. Total Subscriptions
When the SUBSCRIBE, unsubscribe, Psubscribe, and Punsubscribe commands are executed, the last element that returns the result is the total number of channels and modes that the client is still subscribing to.
When the client withdraws all channels and modes, that is, when the total value drops to 0, the client exits the subscription and publish status. Programming examples
Pieter Noordhuis provides a high-performance multi-user web chat software written using Eventmachine and Redis, which is a good showcase for the use of publishing and subscription capabilities. Client Library Implementation Hints
Because all received information will contain a source of information: When the information comes from the channel, the source is a channel, and when the information comes from the schema, the source is a pattern.
Therefore, a client can use a hash table to associate a particular source with a callback function that handles the source. When new information arrives, the program can process the information to the correct callback function according to the source of the information, within the O (1) complexity.
From:http://redisdoc.com/topic/pubsub.html