当前位置:网站首页>17_ Redis_ Redis publish subscription
17_ Redis_ Redis publish subscription
2022-07-02 15:20:00 【Listen to the rain】
Redis Publish subscribe (pub/sub) It's a kind of Message communication mode ∶ sender (pub) Send a message , subscriber (sub) receive messages .
Redis Clients can subscribe to any number of channels . for example : A user can pay attention to as many as he wants up Lord
subscribe / Release message map ∶
first : Message sender
the second : channel
Third : Message subscribers 
The picture below shows the channel channel1, And three clients that subscribe to this channel ―—client2、 client5 and client1 The relationship between ∶
When new news passes PUBLISH Command to channel channel1 when , This message will be sent to the three clients who subscribe to it :

command : These commands are widely used to build instant messaging applications , Such as Internet chat room (chatroom) And real-time broadcasting 、 Real time reminder, etc .

test
Subscribers :
127.0.0.1:6379> SUBSCRIBE xsy666 // Subscribe to a channel :xsy666
Reading messages… (press Ctrl-C to quit)
1 ) “subscribe”
2 ) “xsy666”
3 ) (integer) 1
// Waiting to read the push message
1 ) “message” // news
2 ) “xsy666” // Which channel
3 ) “hello,xsy” // The details of the message
1 ) “message”
2 ) “xsy666”
3 ) “hello,zhangsan”
1 ) “message”
2 ) “xsy666”
3 ) “byebye”
The sender :
127.0.0.1:6379> PUBLISH xsy666 hello,xsy // Publishers post messages to channels !
(integer) 1
127.0.0.1:6379> PUBLISH xsy666 hello,zhangsan
(integer) 1
127.0.0.1:6379> PUBLISH xsy666 byebye
(integer) 1
principle
Redis It's using C Realized , Through analysis Redis Source code pubsub.c file , Understand the underlying implementation of publish and subscribe mechanisms , In order to deepen the understanding of Redis The understanding of the .
Redis adopt PUBLISH、SUBSCRIBE and PSUBSCRIBE And so on to realize the function of publish and subscribe .
adopt SUBSCRIBE After subscribing to a channel ,redis-server It maintains a dictionary , The key of the dictionary is one by one channel, The value of a dictionary is a linked list , The linked list holds all the subscriptions channel The client of .SUBSCRIBE The key to command , Is to add the client to the given channel In the subscription list of .
adopt PUBLISH Command to send messages to subscribers ,redis-server The given channel is used as the key , In what it maintains channel Look up the list of all clients who subscribe to this channel in the dictionary , Traverse the list , Publish the message to all subscribers .
Pub/Sub Literally, it's publishing ( Publish ) With subscription ( Subscribe ), stay Redis in , You can set to a certain key Value for message publishing and message subscription , When one key After the news is released on the value , All clients that subscribe to it will receive the corresponding message . The most obvious use of this feature is as a real-time messaging system , For example, ordinary instant chat , Group chat and other functions .
Use scenarios :
1. Real time message system
2. Live chat
3. subscribe , Focus on
边栏推荐
猜你喜欢

Practice of compiling principle course -- implementing an interpreter or compiler of elementary function operation language

20_Redis_哨兵模式
![[c voice] explain the advanced pointer and points for attention (2)](/img/fb/515e25899bd9a2905ee63cb041934a.png)
[c voice] explain the advanced pointer and points for attention (2)

【C语言】详解指针的初阶和进阶以及注意点(1)

c语言入门--数组

Jenkins Pipeline 应用与实践

【C语音】详解指针进阶和注意点(2)

MFC timer usage

03_线性表_链表

21_ Redis_ Analysis of redis cache penetration and avalanche
随机推荐
LeetCode_ String_ Simple_ 412.Fizz Buzz
MFC timer usage
Tidb cross data center deployment topology
Map介绍
LeetCode 2320. Count the number of ways to place the house
CodeCraft-22 and Codeforces Round #795 (Div. 2)D,E
How to conduct TPC-C test on tidb
Introduction to C language -- array
The traversal methods of binary tree mainly include: first order traversal, middle order traversal, second order traversal, and hierarchical traversal. First order, middle order, and second order actu
Learn the method code of using PHP to realize the conversion of Gregorian calendar and lunar calendar
[c voice] explain the advanced pointer and points for attention (2)
C # delay, start the timer in the thread, and obtain the system time
[solution] educational codeforces round 82
19_Redis_宕机后手动配置主机
學習使用php實現公曆農曆轉換的方法代碼
13_Redis_事务
Map introduction
The past and present lives of visual page building tools
编译原理课程实践——实现一个初等函数运算语言的解释器或编译器
14_Redis_乐观锁