当前位置:网站首页>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) 1As 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.
边栏推荐
猜你喜欢
随机推荐
What does the errorPage attribute of the JSP page directive do?
重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件
els 方块边界变形处理
构造方法,this关键字,方法的重载,局部变量与成员变量
TCL: Pin Constraints Using the tcl Scripting Language in Quartus
go语言标准库fmt包怎么使用
632. Minimum interval
图解LeetCode——1161. 最大层内元素和(难度:中等)
GetHashCode方法与=
Grid false data injection attacks detection based on coding strategy
Day11 shell脚本基础知识
els 方块变形判断。
Axure tutorial - the new base (small white strongly recommended!!!)
BGP 第一次实验
When Netflix's NFTs Forget Web2 Business Security
[Headline] Written test questions - minimum stack
解析正则表达式的底层实现原理
Arduino Basic Syntax
工业信息物理系统攻击检测增强模型



![[头条]笔试题——最小栈](/img/67/08f2be8afc780e3848371a1b5e04db.png)





