当前位置:网站首页>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.
边栏推荐
- JSP内置对象out对象的功能简介说明
- Using the "stack" fast computing -- reverse polish expression
- 不要用jOOQ串联字符串
- PHP从txt文件中读取数据的方法
- 460. LFU 缓存
- JSP如何使用request获取当前访问者的真实IP呢?
- ROS 动态参数
- [Headline] Written test questions - minimum stack
- [HCIP] BGP Small Experiment (Federation, Optimization)
- 在不完全恢复、控制文件被创建或还原后,必须使用 RESETLOGS 打开数据库,解释 RESETLOGS.
猜你喜欢
Arduino 基础语法
为什么要使用MQ消息中间件?这几个问题必须拿下
这 4 款电脑记事本软件,得试试
实现删除-一个字符串中的指定字母,如:字符串“abcd”,删除其中的”a”字母,剩余”bcd”,也可以传递多个需要删除的字符,传递”ab”也可以做到删除”ab”,剩余”cd”。
[21-Day Learning Challenge] A small summary of sequential search and binary search
玩转NFT夏季:这份工具宝典值得收藏
什么是低代码(Low-Code)?低代码适用于哪些场景?
BGP 第一次实验
An interesting project--Folder comparison tool (1)
控制电机的几种控制电路原理图
随机推荐
[21-Day Learning Challenge] A small summary of sequential search and binary search
How to design a circular queue?Come and learn~
go语言标准库fmt包怎么使用
JSP page指令errorPage属性起什么作用呢?
CRS 管理与维护
Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
460. LFU 缓存
REST会消失吗?事件驱动架构如何搭建?
PHP从txt文件中读取数据的方法
面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
els block deformation judgment.
测试用例:四步测试设计法
含外部储能的电力系统暂态稳定分布式控制
已知中序遍历数组和先序遍历数组,返回后序遗历数组
632. Minimum interval
JSP out.write()方法具有什么功能呢?
ROS dynamic parameters
基于注意力机制的多特征融合人脸活体检测
Arduino Basic Syntax