Redis Publish Subscriptions

Source: Internet
Author: User

A Redis Publish Subscription (PUB/SUB) is a message communication pattern: the Sender (pub) sends a message and the Subscriber (sub) receives the message.

Redis clients can subscribe to any number of channels.

Shows the relationship between channel Channel1 and the three client--client2, CLIENT5, and client1 subscribing to this channel:

When a new message is sent to channel Channel1 via the PUBLISH command, the message is sent to the three clients subscribed to it:

Instance

The following example shows how a publish subscription works. In our example we created a subscription channel named Redischat:

  1. Redis 127.0. 0.1:6379> SUBSCRIBE redischat
  2. Reading Messages... (press Ctrl-C to quit)
  3. 1) "Subscribe"
  4. 2) "Redischat"
  5. 3) (integer) 1

Now, we re-open a Redis client and then publish two messages on the same channel Redischat, and the Subscriber can receive the message.

  1. Redis 127.0. 0.1:6379> PUBLISH redischat "Redis is a great caching technique" /c2>
  2. (integer) 1
  3. Redis 127.0. 0.1:6379> PUBLISH redischat "Learn Redis by w3cschool.cc"
  4. (integer) 1
  5. # The subscriber's client will display the following message
  6. 1) "message"
  7. 2) "Redischat"
  8. 3) "Redis is a great caching technique"
  9. 1) "message"
  10. 2) "Redischat"
  11. 3) "Learn Redis by w3cschool.cc"
Redis Publish Subscription commands

The following table lists the common commands for Redis publish subscriptions:

Serial Number Command and Description
1 Psubscribe pattern [pattern ...] subscribes to one or more channels that match a given pattern.
2 PUBSUB subcommand [argument [argument ...]] View subscription and release system status.
3 The PUBLISH channel message sends information to the specified channel.
4 Punsubscribe [pattern [pattern ...]] unsubscribe from all channels of a given pattern.
5 SUBSCRIBE Channel [Channel ...] subscribe to the information given for one or more channels.
6 unsubscribe [Channel [channel ...]] means to unsubscribe from a given channel.

Redis Publish Subscriptions

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.