Basic Tutorials of Redis(7) -Publish and Subscribe
This post is mainly about the publishment and subscription in Redis.I think you may subscribe some offiial
accounts on wechat,and when the authors published something new to their accounts,you will get them in
your wechat.The instance can make you understand this pattern easily.You will find there only 6 commands
for publishment and subscription.
This post introduces the basic usages as well.I will show you how to publish a message to a channel.publish
is the command to publish sometings.I publish a channel named news-nba with the message nba.And the client
return me a integer 0.What is the meaning of the return value?It means that there is nobody subscribe this channel.
publish news-nba nba
I open a new client for subscriber.In this client,I subscribe the above channel news-nba.It will return something
about this channel.
subscribe news-nba
OK,let's publish a new message to the news-nba to see the changes in the both clients.After publishing lakers to
the news-nba channel,this client returns 1.It means that the channel has a subscriber.
publish news-nba lakers
Let's turn to the subscriber's client.You will find the message lakers was already in the client.So amazing it is.
publish a new message to the news-nba,it returns 2 meaning ...(you understand it)
All right,let's see the other commands of this feature in Redis.Seeing the sql first:
select * from channels where channelname like 'news%' To execute this sql,you will get all of the channel whoes
name start with news.Redis can also do that to help us subscibe many channels by matching their name vaguely.
For example I want to subscribe all the channels of news.I will use psubscribe to do this job.
psubscribe news-*
publish news-tech tech
The subscriber's client will receive the message from the channel news-tech.
Again!!Publishing a message to a new channel.
As you can see,the client receives this message as well.
following code demonstrates the usage in C#.
1 //publisher 2 var sub = redis.GetSubscriber(); 3 //subscriber 4 var sub2 = redis.GetSubscriber(); 5 6 sub.Publish("news-nba", "nba"); 7 sub.Publish("news-tech", "tech"); 8 9 //sub 10 sub2.Subscribe("news-nba", (channel, value) => 11 { 12 Console.WriteLine(string.Format("{0} has been published to {1}!",value,channel)); 13 }); 14 sub2.Subscribe("news-tech", (channel, value) => 15 { 16 Console.WriteLine(string.Format("{0} has been published to {1}!", value, channel)); 17 }); 18 19 sub.Publish("news-nba", "Lakers"); 20 sub.Publish("news-nba", "Kobe"); 21 22 sub.Publish("news-tech", "lumia"); 23 24 System.Threading.Thread.Sleep(2000); 25 Console.WriteLine("unscribe the news-nba"); 26 sub2.Unsubscribe("news-nba");//unsub 27 28 sub.Publish("news-tech", "surface pro3"); 29 30 System.Threading.Thread.Sleep(2000); 31 sub.Publish("news-nba", "james"); 32 33 System.Threading.Thread.Sleep(2000); 34 Console.WriteLine("unscribe all channels"); 35 sub2.UnsubscribeAll(); 36 37 sub.Publish("news-nba", "paul"); 38 sub.Publish("news-tech", "surface pro4");

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述