当前位置:网站首页>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
边栏推荐
- 面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
- 07_哈希
- Application and practice of Jenkins pipeline
- 记一次面试
- Dragonfly low code security tool platform development path
- 编译原理课程实践——实现一个初等函数运算语言的解释器或编译器
- Data analysis thinking analysis methods and business knowledge - business indicators
- 飞凌嵌入式RZ/G2L处理器核心板及开发板上手评测
- 传感器数据怎么写入电脑数据库
- 搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!
猜你喜欢
![[noi Simulation Competition] scraping (dynamic planning)](/img/ee/27a07f80207a2925f5065e633eb39f.png)
[noi Simulation Competition] scraping (dynamic planning)

数据分析思维分析方法和业务知识——业务指标

vChain: Enabling Verifiable Boolean Range Queries over Blockchain Databases(sigmod‘2019)

搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!

03_ Linear table_ Linked list

工程师评测 | RK3568开发板上手测试

FPGA - 7系列 FPGA内部结构之Clocking -03- 时钟管理模块(CMT)

编译原理课程实践——实现一个初等函数运算语言的解释器或编译器

The past and present lives of visual page building tools

面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
随机推荐
Common English abbreviations for data analysis (I)
QML pop-up frame, customizable
学习使用php实现公历农历转换的方法代码
解决el-radio-group 回显后不能编辑问题
php获取数组中键值最大数组项的索引值的方法
学习使用php将时间戳转换为大写日期的方法代码示例
TiDB 软件和硬件环境建议配置
Record an error report, solve the experience, rely on repetition
Leetcode - Search 2D matrix
MFC 定时器使用
MFC CString 转 char*
记一次面试
如何对 TiDB 进行 TPC-C 测试
02_线性表_顺序表
.NET Core 日志系统
02_ Linear table_ Sequence table
List集合&UML图
Learn the method code example of converting timestamp to uppercase date using PHP
08_ 串
C#延时、在线程中开启定时器、获取系统时间