Pub/Sub 发布订阅

Related commands

相关命令

PSUBSCRIBE

PUBLISH

PUBSUB

PUNSUBSCRIBE

SUBSCRIBE

UNSUBSCRIBE

Pub/Subandimplement thewhere (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.

For instance in order to subscribe to channelsfooandbarthe client issues aproviding the names of the channels:

例如,为了订阅通道消息名为foo 和 bar的消息 客户端像通道发送SUBSCRIBE:

SUBSCRIBE foo barMessages 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 theandoperations 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 awith 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.

message: it is a message received as result of aPUBLISHcommand 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$9subscribe$5first:1*3$9subscribe$6second:2operation against the channel namedsecond:同时,其他的客户端发布second通道:> PUBLISH second Hellohis is what the first client receives:第一个客户端将收到:*3$7message$6second$5HelloNow the client unsubscribes itself from all the channels using thecommand without additional arguments:现在客户端使用UNSUBSCRIBE命令不带参数取消自己定义的所有通道:

UNSUBSCRIBE*3$11unsubscribe$6second:1*3$11unsubscribe$5first:0Pattern-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: 例如:

可以有一个人陪着你,也可以你一个人,总之那一刻,

Pub/Sub 发布订阅

相关文章:

你感兴趣的文章:

标签云: