欢迎投稿

今日深度:

Pub/Sub发布订阅

Pub/Sub发布订阅


Related commands 相关命令
PSUBSCRIBE PUBLISH PUBSUB PUNSUBSCRIBE SUBSCRIBE UNSUBSCRIBE

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.
命令SUBSCRIBE,UNSUBSCIBE 和PUBLISH实现了Publish/Subsribe发送消息范例(引用维基百科)也就是发送者(发布者)不为接受者(订阅者)实现消息处理程序细节。当然,发布的消息类型redis称为通道(redis将消息类型称为通道channel),不知道具体的订阅者。订阅者向服务器表达自己感兴趣的一个或者多个通道,然后接受他们感兴趣的消息,并不知道具体的发布者。这样实现了发布和订阅的解耦可以允许系统是可扩展的和可增加更多的网络拓扑。
For instance in order to subscribe to channels foo and bar the client issues a SUBSCRIBE providing the names of the channels:
例如,为了订阅 通道 消息名为 foo 和 bar的消息 客户端像通道发送SUBSCRIBE:
SUBSCRIBE foo bar
Messages sent by other clients to these channels will be pushed by Redis to all the subscribed clients.
如果有该类型的消息通道被其他客户端发布就会推送到所有的订阅客户端。 A client subscribed to one or more channels should not issue commands, although it can subscribe and unsubscribe to and from other channels. The reply of the SUBSCRIBE and UNSUBSCRIBE operations are sent in the form of messages, so that the client can just read a coherent stream of messages where the first element indicates the type of message.
一个客户端已经订阅一个或者多个通道就不应该在发出其他命令,虽然它还可以订阅和取消订阅通道。SUBSCIBE和UNSUBSCRIBE操作的应答在消息里面发送的,以便客户端可以读取消息流,其中的第一个元素表示消息的类型。

Format of pushed messages发布消息格式

A message is a Array reply with three elements.
一个消息是一个有3个元素的应答数组。
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.
取消订阅:意思是我们成功订阅到第二个元素代码的通道(消息类型)。第3个元素代表我们当前已经订阅的通道(消息类型)数量。当第3个元素为0时,我们不在订阅任何通道,并且这个客户端现在可以发送任何Pub/Sub之外的命令。

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.

消息:意思是收到其他客户端使用PUBLISH命令发布的消息。第二个参数表示通道(消息类型),最后第三个参数表示真正的消息内容。

 

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.
pub/sub 与key空间没有关联。没有任何级别的干预,包括各个数据库都没有干预(redis实例默认有10数据库)。 Publishing on db 10, will be heard by a subscriber on db 1.
在db10发布在db1也能监听到。 If you need scoping of some kind, prefix the channels with the name of the environment (test, staging, production, ...).
如果一些类型需要范围,使用名字的前缀(test,staging,production,...)

 

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.
Redis的 pub/sub的实现支持模式匹配。客户端可以使用全局样式订阅并接受所有和给出的模式相匹配的所有通道消息。 For instance: 例如:
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. 类型是pmessage:它是客户端使用PUBLISH命令发布的消息,匹配一个模式匹配订阅。第二个元素代表匹配,第三个元素通道名字的开始,最后一个元素是真正消息内容。 Similarly to SUBSCRIBE and UNSUBSCRIBE, PSUBSCRIBE and PUNSUBSCRIBE commands are acknowledged by the system sending a message of type psubscribe and punsubscribe using the same format as the subscribe andunsubscribe message format 与.SUBSCRIBE和UNSUBSCRIBE一样,PSUBSCRIBE和PUNSUBSCRUBE命令使用与subscribe 和unsubscribe发送相同的消息格式发送消息也是允许的。

Messages matching both a pattern and a channel subscription匹配订阅和一般订阅


A client may receive a single message multiple times if it's subscribed to multiple patterns matching a published message, or if it is subscribed to both patterns and channels matching the message. Like in the following example:
一个客户端如果订阅多匹配模式发布的消息,或者如果订阅了两个模式和通道匹配的消息,那么这个客户端可能会收到一个消息多次。就像下面这样:
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 messageand one of type pmessage.
在上面的例子,如果一个foo通道的消息被发送,这个客户端将会收到两个消息:一个类型是messageand 另一个类型是pmessage

The meaning of the subscription count with pattern matching订阅数量的意义

In 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 实现实例

Pieter Noordhuis provided a great example using EventMachine and Redis to create a multi user high performance web chat.
Pieter Noordhuis使用redis的EventMachine创建了一个多用户的高性能网络聊天系统。

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 注册订阅通道与回调函数关联,当收到订阅通道消息时直接调用注册的函数处理。

www.htsjk.Com true http://www.htsjk.com/sybase/19770.html NewsArticle Pub/Sub发布订阅 Related commands 相关命令 PSUBSCRIBE PUBLISH PUBSUB PUNSUBSCRIBE SUBSCRIBE UNSUBSCRIBE Pub/Sub SUBSCRIBE, UNSUBSCRIBE and PUBLISH implement the Publish/Subscribe messaging paradigm where (citing Wikipedia) senders (p...
评论暂时关闭