Redis's pub/sub command

Source: Internet
Author: User
Tags sub command

Redis Publish Subscriptions

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:

Redis 127.0.0.1:6379> SUBSCRIBE redischatreading messages ... (Press Ctrl-c to quit) 1) "Subscribe" 2) "Redischat" 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.

Redis 127.0.0.1:6379> PUBLISH redischat "Redis is a great Caching technique" (integer) 1redis 127.0.0.1:6379> PUBLISH Redischat "Learn Redis by w3cschool.cn" (integer) # Subscriber will display the following message 1) "Message" 2) "Redischat" 3) "Redis is a great caching Technique "1)" "Message" 2) "Redischat" 3) "Learn Redis by w3cschool.cn"



The Psubscribe command subscribes to one or more channels that match a given pattern. Each pattern takes a * as a match, for example it* matches all channels that start with it (It.news, It.blog, It.tweets, and so on). News.* matches all with news. The first channel (news.it, News.global.today, etc.), and so forth.

Redis 127.0.0.1:6379> psubscribe pattern [pattern ...]

return value
The information received.

Instance
Redis 127.0.0.1:6379> Psubscribe MyChannel
Reading messages ... (Press Ctrl-c to quit)
1) "Psubscribe"
2) "MyChannel"
3) (integer) 1

The Pubsub command is used to view the status of the subscription and publishing system, which consists of several sub-commands in different formats.

Redis 127.0.0.1:6379> PUBSUB <subcommand> [argument [argument ...]

return value
A list of active channels.

Instance
Redis 127.0.0.1:6379> PUBSUB CHANNELS
(empty list or set)


The Publish command is used to send information to the specified channel.

Redis 127.0.0.1:6379> PUBLISH Channel message

return value
The number of subscribers who received the information.

Instance
Redis 127.0.0.1:6379> PUBLISH mychannel "Hello, i m here"
(integer) 1

Iv. The Punsubscribe command is used to unsubscribe from all channels of a given pattern.

Redis 127.0.0.1:6379> Punsubscribe [pattern [pattern ...]]

return value
This command has different performance on different clients.

Instance
Redis 127.0.0.1:6379> Punsubscribe MyChannel
1) "Punsubscribe"
2) "A"
3) (integer) 1


V. The Subscribe command is used to subscribe to information for a given channel or channels.

Redis 127.0.0.1:6379> SUBSCRIBE Channel [channel ...]

return value
Received information

Instance
Redis 127.0.0.1:6379> SUBSCRIBE MyChannel
Reading messages ... (Press Ctrl-c to quit)
1) "Subscribe"
2) "MyChannel"
3) (integer) 1
1) "Message"
2) "MyChannel"
3) "A"


Vi. The unsubscribe command is used to unsubscribe information for a given channel or channels.

Redis 127.0.0.1:6379> unsubscribe Channel [channel ...]

return value
This command has different performance on different clients.

Instance
Redis 127.0.0.1:6379> Unsubscribe MyChannel
1) "Unsubscribe"
2) "A"
3) (integer) 0



Redis's pub/sub command

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.