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#.
//publisher
var sub = redis.GetSubscriber();
//subscriber
var sub2 = redis.GetSubscriber(); sub.Publish("news-nba", "nba");
sub.Publish("news-tech", "tech"); //sub
sub2.Subscribe("news-nba", (channel, value) =>
{
Console.WriteLine(string.Format("{0} has been published to {1}!",value,channel));
});
sub2.Subscribe("news-tech", (channel, value) =>
{
Console.WriteLine(string.Format("{0} has been published to {1}!", value, channel));
}); sub.Publish("news-nba", "Lakers");
sub.Publish("news-nba", "Kobe"); sub.Publish("news-tech", "lumia"); System.Threading.Thread.Sleep();
Console.WriteLine("unscribe the news-nba");
sub2.Unsubscribe("news-nba");//unsub sub.Publish("news-tech", "surface pro3"); System.Threading.Thread.Sleep();
sub.Publish("news-nba", "james"); System.Threading.Thread.Sleep();
Console.WriteLine("unscribe all channels");
sub2.UnsubscribeAll(); sub.Publish("news-nba", "paul");
sub.Publish("news-tech", "surface pro4");

Basic Tutorials of Redis(7) -Publish and Subscribe的更多相关文章
- Basic Tutorials of Redis(9) -First Edition RedisHelper
After learning the basic opreation of Redis,we should take some time to summarize the usage. And I w ...
- Basic Tutorials of Redis(6) - List
Redis's List is different from C#'s List,but similar with C#'s LinkedList.Sometimes I confuse with t ...
- Basic Tutorials of Redis(2) - String
This post is mainly about how to use the commands to handle the Strings of Redis.And I will show you ...
- dotnet core use Redis to publish and subscribe
安装Redis 同样我这边再次使用Docker, 方便快捷: # 拉取镜像 docker pull redis # 运行镜像 docker run -d -p 6379:6379 --name red ...
- Basic Tutorials of Redis(8) -Transaction
Data play an important part in our project,how can we ensure correctness of the data and prevent the ...
- Basic Tutorials of Redis(5) - Sorted Set
The last post is mainly about the unsorted set,in this post I will show you the sorted set playing a ...
- Basic Tutorials of Redis(4) -Set
This post will introduce you to some usages of Set in Redis.The Set is a unordered set,it means that ...
- Basic Tutorials of Redis(3) -Hash
When you first saw the name of Hash,what do you think?HashSet,HashTable or other data structs of C#? ...
- Basic Tutorials of Redis(1) - Install And Configure Redis
Nowaday, Redis became more and more popular , many projects use it in the cache module and the store ...
随机推荐
- Objective-C中block的底层原理
先出2个考题: 1. 上面打印的是几,captureNum2 出去作用域后是否被销毁?为什么? 同样类型的题目: 问:打印的数字为多少? 有人会回答:mutArray是captureObject方法的 ...
- 深入理解 JavaScript,以及 Linux 下的开发调试工具
前言 JavaScript 是我接触到的第二门编程语言,第一门是 C 语言.然后才是 C++.Java 还有其它一些什么.所以我对 JavaScript 是非常有感情的,毕竟使用它有十多年了.早就想写 ...
- Android Weekly Notes Issue #237
Android Weekly Issue #237 December 25th, 2016 Android Weekly Issue #237 这是本年的最后一篇issue, 感谢大家. 本期内容包括 ...
- 当我们在谈论kmeans(1)
本稿为初稿,后续可能还会修改:如果转载,请务必保留源地址,非常感谢! 博客园:http://www.cnblogs.com/data-miner/ 简书:建设中... 知乎:建设中... 当我们在谈论 ...
- Struts框架的核心业务
Struts的核心业务 Struts核心业务有很多,这里主要介绍了比较简单一些的: 请求数据的处理,和数据自动封装,类型自动转换 1.Struts中数据处理 1.1.方式1:直接过去servletap ...
- SpringMvc中的数据校验
SpringMvc中的数据校验 Hibernate校验框架中提供了很多注解的校验,如下: 注解 运行时检查 @AssertFalse 被注解的元素必须为false @AssertTrue 被注解的元素 ...
- 【腾讯Bugly干货分享】OCS——史上最疯狂的iOS动态化方案
本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/zctwM2Wf8c6_sxT_0yZvXg 导语 在 ...
- 吸顶大法 -- UWP中的工具栏吸顶的实现方式之一
如果一个页面中有很长的列表/内容,很多应用都会在用户向下滚动时隐藏页面的头,给用户留出更多的阅读空间,同时提供一个方便的吸顶工具栏,比如淘宝中的店铺页面. 下面是一个比较简单的实现,如果有同学有更好的 ...
- Team Leader 你不再只是编码, 来炖一锅石头汤吧
h3{ color: #000; padding: 5px; margin-bottom: 10px; font-weight: bolder; background-color: #ccc; } h ...
- Java 开发主流 IDE 环境体验
前言 本来应该继续从 Oracle 官网搬砖的,但是随着示例代码越来越复杂,涉及的类库越来越多,使用 Vim 写 Java 代码就很力不从心了,是时候上 IDE 了.我最熟悉的 IDE 环境是 Ecl ...