当前位置:网站首页>Redis - message publish and subscribe
Redis - message publish and subscribe
2022-08-02 00:32:00 【Even though the sunset is not as beautiful as you】
Foreword
Redis publish-subscribe (pub/sub) is a message communication model: sender (pub) sends messages and subscribers (sub) receive messages.
Redis clients can subscribe to any number of channels!
First, the implementation method:
①Commands:
These commands are widely used to build instant messaging applications, such as online chat rooms (chatroom) and real-time broadcasting, real-time reminders, etc.
②Implementation of publish and subscribe:
1. Subscriber:
127.0.0.1:6379> pingPONG127.0.0.1:6379> SUBSCRIBE dingdada #Subscribe to the channel named dingdadaReading messages... (press Ctrl-C to quit)1) "subscribe"2) "dingdada"3) (integer) 1#Waiting for push information1) "message" #message2) "dingdada" #The message from which channel3) "hello world\xef\xbc\x81" # The specific content of the message1) "message"2) "dingdada"3) "my name is dyj\x81"
2. Sender:
127.0.0.1:6379> pingPONG127.0.0.1:6379> PUBLISH dingdada "hello world!" #Send message to dingdada channel(integer) 1127.0.0.1:6379> PUBLISH dingdada "my name is dyj" #Send message to dingdada channel(integer) 1
As shown in the picture:
Subscriber:
Sender:
③PSUBSCRIBE command: subscribe to the specified channel!
③PSUBSCRIBE command: subscribe to the specified channel!
④PUBLISH command: send a message to the specified channel!
PUBLISH + channel + message #Send information message to the specified channel channel
⑤PUNSUBSCRIBE command: unsubscribe!
#Instructs the client to unsubscribe from the specified mode, and exits all modes if no mode is provided.
⑥SUBSCRIBE: Subscription, same as above.Don't go into details!
⑦UNSUBSCRIBE: Unsubscribe, same as above, no details!
⑧Summary:
Pub/Sub literally means Publish and Subscribe. In Redis, you can set message publishing and message subscription for a certain key value. When a message is published on a key valueAfter that, all clients that subscribe to it will receive the corresponding message.The most obvious usage of this function is as a real-time messaging system, such as ordinary instant chat, group chat and other functions.
边栏推荐
- 辨析内存函数memset、memcmp、memmove以及memcpy
- Multi-feature fusion face detection based on attention mechanism
- [HCIP] BGP Small Experiment (Federation, Optimization)
- 基于注意力机制的多特征融合人脸活体检测
- Disk and file system management
- Cyber-Physical System State Estimation and Sensor Attack Detection
- Study Notes: The Return of Machine Learning
- [Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core
- 玩转NFT夏季:这份工具宝典值得收藏
- 链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?
猜你喜欢
随机推荐
电机原理动图合集
The Statement update Statement execution
基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
玩转NFT夏季:这份工具宝典值得收藏
认识USB、Type-C、闪电、雷电接口
如何优雅的消除系统重复代码
146. LRU cache
632. 最小区间
08-SDRAM:汇总
如何设计循环队列?快进来学习~
QML package management
测试点等同于测试用例吗
面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
这 4 款电脑记事本软件,得试试
Transient Stability Distributed Control of Power System with External Energy Storage
els 方块边界变形处理
els block boundary deformation processing
【解决】win10下emqx启动报错Unable to load emulator DLL、node.db_role = EMQX_NODE__DB_ROLE = core
Keepalived 高可用的三种路由方案
BGP 第一次实验