Pub/Sub发布订阅(一)

2015-07-24 06:57:32 · 作者: · 浏览: 0
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