Pub/Sub 發布訂閱,pubsub發布訂閱

來源:互聯網
上載者:User

Pub/Sub 發布訂閱,pubsub發布訂閱
Related commands相關命令
PSUBSCRIBEPUBLISHPUBSUBPUNSUBSCRIBESUBSCRIBEUNSUBSCRIBE
Pub/SubSUBSCRIBE, 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$9subscribe$5first:1*3$9subscribe$6second: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$7message$6second$5Hello
    Now the client unsubscribes itself from all the channels using the UNSUBSCRIBE command without additional arguments:
    現在用戶端使用UNSUBSCRIBE命令不帶參數取消自己定義的所有通道:
    UNSUBSCRIBE*3$11unsubscribe$6second:1*3$11unsubscribe$5first: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.figurativenews.music.jazz, etc. All the glob-style patterns are valid, so multiple wildcards are supported.
    將被發送到的通道為  news.art.figurativenews.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 and unsubscribe 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 fooPSUBSCRIBE 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 另一個類型是pmessageThe meaning of the subscription count with pattern matching訂閱數量的意義In  subscribeunsubscribepsubscribe 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.
    subscribeunsubscribepsubscribe 和 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 註冊訂閱通道與回呼函數關聯,當收到訂閱通道訊息時直接調用註冊的函數處理。


    相關文章

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.