Redis study notes (XI) --- publish and subscribe

1 Introduction

Redis publish-subscribe (pub / sub) is a messaging mode of communication. Sender (Pub) sends a message, the subscriber (Sub) receiving a message
Redis client may subscribe to any number of channels

2. Example

The following shows three channels channel1 client and subscribe to this channel ---- relationship between client2, client5 and client1:
Here Insert Picture Description
When a message is sent through the channel 1 channel PUBLISH command, this message will be sent to subscribe to it three clients.
Here Insert Picture Description

3. Command

Subscriptions

  subscribe channel [channel...]        #订阅给定的一个或多个频道的消息
  psubscribe pattern [pattern....]      # 订阅一个或多个给定模式的频道

Release channel

 publish channel message   #将信息发送到指定的频道

Unsubscribe Channel

unsubscribe [channel [channel...]]         #指退订给定的频道
punsubscribe [pattern [pattern]]           #退订所有给定模式的频道

Code Example 4

[1] first three windows simultaneously open (wherein two analog subscribers, a publisher analog)

Here Insert Picture Description
[2] The first channel operates in two subscribers subscribe windows
suscribe CCTV5
Here Insert Picture Description
Here Insert Picture Description
[. 3] in the third window perform post operation (FIG. 2 represents two Integer received subscriber information)
publish CCTV5 the NBA
Here Insert Picture Description
[. 4] This two subscribers will also receive information when (then three windows)

Here Insert Picture Description
Subscribe to the window display interface
Here Insert Picture Description

The application scenario

The most obvious use of this feature is to build real-time messaging systems, such as ordinary instant messaging, group chat and other functions
[1] In a blog site, there are 100 fans subscribed to you, when you publish a new post, you can push message to the fans.
[2] The micro-channel public number mode

Published 41 original articles · won praise 2 · Views 1946

Guess you like

Origin blog.csdn.net/TheWindOfSon/article/details/104269818
Recommended