Advanced practical features of Redis--Publish and subscribe messages

Source: Internet
Author: User
Tags redis server
A publish Subscription (PUB/SUB) is a message traffic pattern, with the main purpose of decoupling the coupling between the message publisher and the message Subscriber, which is similar to the Observer pattern in design mode. Pub/sub not only solves the direct code level coupling between publishers and subscribers, but also solves the coupling of both in physical deployment. As a pub/sub server, Redis functions as a message route between subscribers and publishers. Subscribers can subscribe to Redis server via the Subscribe and Psubscribe commands to a message type that is of interest to them, redis the message type as a channel (channel). When the publisher sends a specific type of message to Redis server through the Publish command. All the client subscribing to this message type will receive this message. There are many pairs of messages passing here. A client can subscribe to multiple channel, or it can send messages to multiple channel.

Let's do an experiment. Using 3 different client, CLIENT1 is used to subscribe to TV1 this channel message, CLIENT2 to subscribe to TV1 and TV2 2 Chanel messages, client3 for publishing TV1 and TV2 messages.



The above example will be explained in detail below.

1, Client1 subscribed to TV1 this channel this channel message, CLIENT2 subscribed to TV1 and TV2 2 channels of information

2, Client3 is used to publish TV1 and TV2 the 2 channels of the message publisher

3, Next we in Client3 released a message "Publish TV1 program1", you can see this message is sent to TV1 this channel

4, of course, CLIENT1 and Client2 all received the news of this channel

5, then Client3 also released a message "Publish TV2 program2", this message is sent to TV2, because CLIENT1 did not subscribe to TV1, so the results of the CLIENT1 did not show any results, but client2 subscribe to this channel, So Client2 is going to have a return result.

We can also bulk subscribe to the content of a channel that starts with TV in Psubscribe tv* way.

After reading this small example should have a perceptual understanding of the Pub/sub function. Note that when a connection passes through the Subscribe or Psubscribe subscription channel, it enters the subscription mode. In this mode, you cannot send additional commands except to subscribe to additional channels or to exit subscription mode with unsubscribe or Punsubscribe commands. Also use the Psubscribe command to subscribe to multiple wildcard channels, and if a message matches multiple channel modes, the same message is received multiple times.

Here is the implementation code for PHP:

    <?php  
      
    $redis = new Redis ();  
    $redis->connect (' 127.0.0.1 ', 6379);  
      
    $channel = $argv [1]; Channel  
    $msg = $argv [2];//msg  
      
    $redis->publish (' channel '. $channel, $msg);  
      
    ? >  

    <?php  
      
    $redis = new Redis ();  
    $redis->connect (' 127.0.0.1 ', 6379);  
    $channel = $argv [1]; Channel  
    $redis->subscribe (Array (' channel '. $channel), ' callback ');  
      
    function callback ($instance, $channelName, $message) {  
     echo $channelName, "==>", $message, Php_eol  
    }  
      
    ? >  

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.