Basic Tutorials of Redis (7)-publish and Subscribe

Source: Internet
Author: User

This post was mainly about the publishment and subscription in redis.i think your may subscribe some offiial

Accounts on Wechat,and If the authors published something new to their accounts,you would get them in

Your WeChat. The instance can make your understand this pattern easily. You'll find there only 6 commands

For publishment and subscription.

This post introduces the basic usages as well. I'll show 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 was 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'll return something

About the 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 the channel has a subscriber.

Publish News-nba Lakers

Let's turn to the Subscriber ' s client. You'll find the message Lakers is already in the client. So amazing it is.

  

Opening the second client to subscribe this channel,and you'll find something similar with the above example.

  

Publish a new message to the News-nba,it returns 2 meaning ... (You understand it)

  

The both subscribers ' client is as follows:

  

  

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'll get all of the channel Whoes

Name start with news. Redis can also do, help US subscibe many channels by matching their name vaguely.

For example I want to subscribe all the channels of News. I 'll use the psubscribe to does this job.

Psubscribe news-*

Let's publish some message to the others channel started with news-to find out the changes.
Publish News-Tech Tech

The subscriber's client would receive the message from the channel News-tech.

again!! Publishing a message to a new channel.

As can see,the client receives this message as well.

  The client of Redis can not show you the unsubscription. So turning to the StackExchange.Redis.The

Following code demonstrates the usage in C #.

1             //Publisher2             varSub =Redis. Getsubscriber ();3             //Subscriber4             varSUB2 =Redis. Getsubscriber ();5             6Sub. Publish ("News-nba","NBA");7Sub. Publish ("News-tech","Tech");8            9             //SubTenSub2. Subscribe ("News-nba", (channel, value) = = One             { AConsole.WriteLine (string. Format ("{0} have been published to {1}!", Value,channel)); -             }); -Sub2. Subscribe ("News-tech", (channel, value) = = the             { -Console.WriteLine (string. Format ("{0} have been published to {1}!", value, channel)); -             }); -  +Sub. Publish ("News-nba","Lakers"); -Sub. Publish ("News-nba","Kobe"); +  ASub. Publish ("News-tech","Lumia"); at  -System.Threading.Thread.Sleep ( -); -Console.WriteLine ("Unscribe the News-nba"); -Sub2. Unsubscribe ("News-nba");//unsub -  -Sub. Publish ("News-tech","surface Pro3"); in  -System.Threading.Thread.Sleep ( -); toSub. Publish ("News-nba","James"); +  -System.Threading.Thread.Sleep ( -); theConsole.WriteLine ("Unscribe All Channels"); * Sub2. Unsubscribeall (); $             Panax NotoginsengSub. Publish ("News-nba","Paul"); -Sub. Publish ("News-tech","Surface Pro4");
Debuging the code, you'll see the result as follow.

  

The next post of this series are the basic opreation of transaction in Redis.thanks for your reading.

Basic Tutorials of Redis (7)-publish and Subscribe

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.