Pub/Sub publish and subscribe, pubsub publish and subscribe

Source: Internet
Author: User
Tags glob

Pub/Sub publish and subscribe, pubsub publish and subscribe
Related commands
PSUBSCRIBEPUBLISHPUBSUBPUNSUBSCRIBESUBSCRIBEUNSUBSCRIBE
Pub/SubSUBSCRIBE, UNSUBSCRIBE and PUBLISH implement the Publish/Subscribe messaging paradigm where (citing Protocol) 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.
Command SUBSCRIBE, UNSUBSCIBE and PUBLISH implement the Publish/Subsribe message sending example (reference Wikipedia), that is, the sender (publisher) is not the receiver (subscriber) to implement the details of the message processing program. Of course, the published message type redis is called the channel (redis calls the message type the channel) and does not know the specific subscriber. The subscriber expresses one or more channels of interest to the server, and then accepts the messages they are interested in without knowing the specific publisher. This decoupling of publishing and subscription allows the system to be scalable and add more network topologies.
For instance in order to subscribe to channelsfooAndbarThe client issues a SUBSCRIBE providing the names of the channels:
For example, to SUBSCRIBE to a message client with the message names foo and bar in the channel, send the SUBSCRIBE message as a channel:

SUBSCRIBE foo bar
Messages sent by other clients to these channels will be pushed by Redis to all the subscribed clients.
If a message channel of this type is published by another client, it will be pushed to all subscription clients. A client subscribed to one or more channels shocould 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.
A client that has subscribed to one or more channels should not issue other commands, although it can also subscribe to or unsubscribe to channels. The responses of SUBSCIBE and UNSUBSCRIBE operations are sent in the message so that the client can read the message stream. The first element indicates the type of the message. Format of pushed messages publish message Format A message is a Array reply with three elements.
A message is a response array with three elements.

The first element is the kind of message:

The first element indicates the Message Type:

  • 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.

    Subscription: the channel (Message Type) represented by the second element ). The 3rd elements represent the number of channels (message types) that we have subscribed to currently.

  • 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. 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.
    Unsubscribe: the channel for successfully subscribing to the second element code (Message type ). The 3rd elements represent the number of channels (message types) that we have subscribed to currently. When 3rd elements are 0, we do not subscribe to any channel, and this client can now send any commands other than Pub/Sub.
    • message: It is a message received ed 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.

    Message: the message that is sent by other clients using the PUBLISH command. The second parameter indicates the channel (Message Type), and the third parameter indicates the real message content.
    Database & Scoping database & scope 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 is not associated with the key space. There is no level of intervention, including no intervention on each database (redis instances have 10 databases by default ). Publishing on db 10, will be heard by a subscriber on db 1.
    It can also be monitored when db10 is released on db1. If you need scoping of some kind, prefix the channels with the name of the environment (test, staging, production ,...).
    If some types require a range, use the name prefix (test, staging, production ,...)
    Example of Wire protocol example write protocol
    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:
    At the same time, other clients release the second channel:
    > PUBLISH second Hello
    his is what the first client receives:
    The first client will receive:
    *3$7message$6second$5Hello
    Now the client unsubscribes itself from all the channels using the UNSUBSCRIBE command without additional arguments:
    Now the client uses the UNSUBSCRIBE command to cancel all the channels defined by itself without parameters:
    UNSUBSCRIBE*3$11unsubscribe$6second:1*3$11unsubscribe$5first:0

    Pattern-matching subscriptions pattern matches to subscribe to 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.
    The pub/sub Implementation of Redis supports pattern matching. The client can subscribe to and accept all channel messages that match the given mode using the global style. For instance: For example:
    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.
    The channel to be sent to is news.art.figurative, News. music. jazz and so on. The entire global style mode is valid, So multiple wildcards are also supported.
    PUNSUBSCRIBE news.*
    Will then unsubscribe the client from that pattern. No other subscriptions will be affected by this call.
    Cancel the subscription in this matching mode. Only the client that has subscribed to and called to cancel the subscription is affected. Messages received as a result of pattern matching are sent in a different format:
    The format of the message sending type in the matching mode is somewhat different:
    • The type of the message ispmessage: It is a message received ed as result of a PUBLISH command issued by another client, matching a pattern-matching subtasks. 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.
    The message type is pmessage: it is a message published by the client using the PUBLISH command, matching a pattern matching subscription. The second element indicates matching. The third element starts with the channel name, and the last element is the real message content. Similarly to SUBSCRIBE and UNSUBSCRIBE, PSUBSCRIBE and PUNSUBSCRIBE commands are acknowledged by the system sending a message of type psubscribeAnd punsubscribeUsing the same format as subscribeAnd unsubscribeMessage format is the same as. SUBSCRIBE and UNSUBSCRIBE. The PSUBSCRIBE and PUNSUBSCRUBE commands use subscribeIt is also allowed to send messages in the same format as unsubscribe.
    Messages matching both a pattern and a channel subscribe match subscription and general 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:
    If a client subscribes to messages published in Multi-match mode, or subscribes to messages in two modes that match the channel, the client may receive one message multiple times. As shown below:
    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.
    In the preceding example, if a foo channel message is sent, the client will receive two messages: messageAnd another type is pmessageThe meaning of the subscribe count with pattern matching. meaning In subscribe, unsubscribe, psubscribeAnd punsubscribeMessage 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. so the client will exit the Pub/Sub state only when this count drops to zero as a result of unsubscribe from all the channels and patterns.
    In subscribe, unsubscribe, psubscribeAnd punsubscribeIn the message type, the last parameter is changed, which indicates the number of subscriptions. This quantity is actually the total number of current subscription channels of the client. Therefore, if the client cancels all subscriptions, the value will drop to 0.
    Programming example implementation instance Pieter Noordhuis provided a great example using EventMachine and Redis to create a multi user high performance web chat.
    Pieter Noordhuis creates a high-performance network chat system for multiple users using redis EventMachine. Client library implementation hints Client implementation upgrade

    Because all the messages encoded ed contain the original sub‑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 subaccounts to callbacks (that can be anonymous functions, blocks, function pointers), using an hash table.

    Use hash table to register the subscription channel and callback function Association. When receiving the subscription channel message, the registered function is called directly.


    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.