程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SyBase數據庫 >> SyBase教程 >> Pub/Sub發布訂閱

Pub/Sub發布訂閱

編輯:SyBase教程

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 注冊訂閱通道與回調函數關聯,當收到訂閱通道消息時直接調用注冊的函數處理。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved