Pub/Sub
SUBSCRIBE , UNSUBSCRIBE and PUBLISH implement the Publish/Subscribe messaging paradigm where (citing Wikipedia) senders (publishers) are not programmed to send their messages to specific receivers (subscribers). Rather, published messages are characterized into channels, without knowledge of what (if any) subscribers there may be. Subscribers express interest in one or more channels, and only receive messages that are of interest, without knowledge of what (if any) publishers there are. This decoupling of publishers and subscribers can allow for greater scalability and a more dynamic network topology.foo
and bar
the client issues a SUBSCRIBE providing the names of the channels:SUBSCRIBE foo bar
Messages sent by other clients to these channels will be pushed by Redis to all the subscribed clients.
The first element is the kind of message:
第一个元素表示消息的类型:
-
subscribe
: means that we successfully subscribed to the channel given as the second element in the reply. The third argument represents the number of channels we are currently subscribed to.订阅:意思是我们成功订阅到第二个元素代表的通道(消息类型)。第3个元素代表我们当前已经订阅的通道(消息类型)数量。
-
unsubscribe
: means that we successfully unsubscribed from the channel given as second element in the reply. The third argument represents the number of channels we are currently subscribed to. When the last argument is zero, we are no longer subscribed to any channel, and the client can issue any kind of Redis command as we are outside the Pub/Sub state.
-
message
: it is a message received as result of a PUBLISH command issued by another client. The second element is the name of the originating channel, and the third argument is the actual message payload.
Database & Scoping 数据库&范围
Pub/Sub has no relation to the key space. It was made to not interfere with it on any level, including database numbers.
Wire protocol example 写协议例子
SUBSCRIBE first second
*3
$9
subscribe
$5
first
:1
*3
$9
subscribe
$6
second
:2
At this point, from another client we issue a PUBLISH operation against the channel named second
:
同时,其他的客户端发布second通道:
> PUBLISH second Hello
his is what the first client receives:
第一个客户端将收到:
*3
$7
message
$6
second
$5
Hello
Now the client unsubscribes itself from all the channels using the
UNSUBSCRIBE
command without additional arguments:
现在客户端使用UNSUBSCRIBE命令不带参数取消自己定义的所有通道:
UNSUBSCRIBE
*3
$11
unsubscribe
$6
second
:1
*3
$11
unsubscribe
$5
first
:0
Pattern-matching subscriptions 模式匹配订阅
The Redis Pub/Sub implementation supports pattern matching. Clients may subscribe to glob-style patterns in order to receive all the messages sent to channel names matching a given pattern.
PSUBSCRIBE news.*
Will receive all the messages sent to the channel
news.art.figurative
,
news.music.jazz
, etc. All the glob-style patterns are valid, so multiple wildcards are supported.
news.art.figurative
, news.music.jazz等等。整个全局样式模式都是有效的,因此多个通配符也是支持的。
PUNSUBSCRIBE news.*
Will then unsubscribe the client from that pattern. No other subscriptions will be affected by this call.
在该匹配模式中取消订阅。仅仅是已经订阅并且调用取消订阅的这个客户端受影响。
Messages received as a result of pattern matching are sent in a different format:
匹配模式的发送消息类型格式有点不同:
- The type of the message is
pmessage
: it is a message received as result of a PUBLISH command issued by another client, matching a pattern-matching subscription. The second element is the original pattern matched, the third element is the name of the originating channel, and the last element the actual message payload.
psubscribe
and punsubscribe
using the same format as the subscribe
andunsubscribe
message format
subscribe
和unsubscribe发送相同的消息格式发送消息也是允许的。Messages matching both a pattern and a channel subscription匹配订阅和一般订阅
SUBSCRIBE foo
PSUBSCRIBE f*
In the above example, if a message is sent to channel
foo
, the client will receive two messages: one of type
message
and one of type
pmessage
.
message
and 另一个类型是pmessage
The meaning of the subscription count with pattern matching订阅数量的意义
subscribe
,
unsubscribe
,
psubscribe
and
punsubscribe
message types, the last argument is the count of subscriptions still active. This number is actually the total number of channels and patterns the client is still subscribed to. So the client will exit the Pub/Sub state only when this count drops to zero as a result of unsubscription from all the channels and patterns.
在
subscribe
,
unsubscribe
,
psubscribe
和
punsubscribe
的消息类型中,最后一个参数是变化的它表示订阅的数量。这个数量事实上是客户端当前订阅通道的总数。因此如果客户端取消所有的订阅这个值将下降到0.
Programming example 实现实例
Client library implementation hints 客户端实现提升
Because all the messages received contain the original subscription causing the message delivery (the channel in the case of message type, and the original pattern in the case of pmessage type) client libraries may bind the original subscription to callbacks (that can be anonymous functions, blocks, function pointers), using an hash table.
使用hash table 注册订阅通道与回调函数关联,当收到订阅通道消息时直接调用注册的函数处理。