当前位置:网站首页>Redis publish subscription
Redis publish subscription
2022-07-26 05:36:00 【Life goes on and battles go on】
What is it?
Redis Publish subscribe (pub/sub) It's a message communication mode : sender (pub) Release the news , subscriber (sub) receive messages .
Redis Clients can subscribe to any number of channels
subscribe / Release message map :

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 comments are widely used to build instant messaging applications , Such as Internet chat room (chatroom) And real-time broadcasting , Real time reminder, etc

test
The following example demonstrates how a publish subscription works , In our example, we create a subscription channel named redischat;
127.0.0.1:6379> subscribe redischat
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "redischat"
3) (integer) 1
Now? , Let's restart one first reids client , And then on the same channel redischat Two news releases , Subscribers can receive messages
127.0.0.1:6379> publish redischat hello,wyt
(integer) 1
127.0.0.1:6379> publish redischat hello,redis
(integer) 1
# The subscriber's client will display the following message1) "message"
2) "redischat"
3) "hello,wyt"
1) "message"
2) "redischat"
3) "hello,redis"
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 PUBSLISH、SUBSCRIBE and PSUBSCRIBE And so on to realize the function of publish and subscribe
adopt SUBSCRIBE After subscribing to a channel ,reids-server It maintains a dictionary , The key of the dictionary is one by one chnnel, 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
Pub/Sub Building a real-time messaging system
Redis Of Pub/Sub The system can build a real-time message system
For example, many uses Pub/Sub An example of real-time chat system
边栏推荐
- Benji Bananas 开启第二季边玩边赚奖励活动,支持使用多张 Benji 通行证!
- Lamp architecture
- 520 for what? DIY is a high-value RGB clock that girls want to watch
- 这种项目,最好别接!
- STL常用模板库
- Yuancosmos provides a digital social platform for fashion design display
- No background, no education? Is it really hopeless for specialist testers to enter Internet factories?
- no networks found in /etc/cni/net.d
- SQL injection
- 520送什么?DIY一个高颜值RGB时钟,女生看了都想要
猜你喜欢

Redis持久化-RDB

IVR在voip电话系统的应用与价值

Code audit CMS

Real scientific weight loss

MongonDB API使用

If MySQL calculates the current month change / current month increase / year-on-year change / year-on-year increase?

Mongondb API usage

You'd better not take this kind of project!

LAMP架构

Do you really understand fiddler, a necessary tool for testing?
随机推荐
LNMP architecture
OD-Paper【1】:Rich feature hierarchies for accurate object detection and semantic segmentation
如何查看Pod里容器名称
学生如何申请免费IDEA
High frequency electronic circuit review examination questions and answers
Chapter 1 - Construction of development environment
How to conduct test case review
轻量级单片机命令行交互项目,全部开源
Benji Bananas 开启第二季边玩边赚奖励活动,支持使用多张 Benji 通行证!
Yolov3 preparatory work
Use flutter to adjust a color filter for the picture of my little sister
Chapter 2 - getting started
元宇宙为服装设计展示提供数字化社交平台
FTP实验及概述
高频电子线路复习考试题及答案
SSH远程管理
Polymer physics test question bank
87. Disturb string
又一开源神器,值得收藏学习!
Redis发布订阅