Redis Subscription/Release
Reference: http://www.cnblogs.com/mushroom/p/4470006.html,http://www.tuicool.com/articles/ABry2aj,http:// www.cnblogs.com/tinywan/p/5903256.html,http://www.cnblogs.com/linjiqin/p/5733183.html,http:// Redisbook.readthedocs.io/en/latest/feature/pubsub.html
A Redis Publish Subscription (PUB/SUB) is a message communication pattern: the Sender (pub) sends a message and the Subscriber (sub) receives the message.
Redis clients can subscribe to any number of channels.
Shows the relationship between channel Channel1 and the three client--client2, CLIENT5, and client1 subscribing to this channel:
When a new message is sent to channel Channel1 via the PUBLISH command, the message is sent to the three clients subscribed to it:
Instance
The following example shows how a publish subscription works. In our example we created a subscription channel named Redischat:
Redis 127.0.0.1:6379> SUBSCRIBE redischatreading messages ... (Press Ctrl-c to quit) 1) "Subscribe" 2) "Redischat" 3) (integer) 1
Now, we re-open a Redis client and then publish two messages on the same channel Redischat, and the Subscriber can receive the message.
Redis 127.0.0.1:6379> PUBLISH redischat "Redis is a great Caching technique" (integer) 1redis 127.0.0.1:6379> PUBLISH Redischat "Learn Redis by runoob.com" (integer) # Subscriber will display the following message 1) "Message" 2) "Redischat" 3) "Redis is a great caching te Chnique "1)" "Message" 2) "Redischat" 3) "Learn Redis by runoob.com"
Redis Publish Subscription commands
The following table lists the common commands for Redis publish subscriptions:
Serial Number |
Command and Description |
1 |
Psubscribe pattern [pattern ...] subscribes to one or more channels that match a given pattern. |
2 |
PUBSUB subcommand [argument [argument ...]] View subscription and release system status. |
3 |
The PUBLISH channel message sends information to the specified channel. |
4 |
Punsubscribe [pattern [pattern ...]] unsubscribe from all channels of a given pattern. |
5 |
SUBSCRIBE Channel [Channel ...] subscribe to the information given for one or more channels. |
6 |
unsubscribe [Channel [channel ...]] means to unsubscribe from a given channel. |
PHP implementation
Subscribers: sub.php
$redis = new Redis ();
$res = $redis->pconnect (' 127.0.0.1 ', 6379,0);
$redis->auth (' 123456 ');
$channel = $argv [1]; Channel
$redis->subscribe (Array (' channel ' $channel), ' callback ');
function callback ($instance, $channelName, $message) {
Echo $channelName, "==>", $message, Php_eol;
}
How to use: (Command-line operation)
PHP sub.php Test
Note: Test subscribes to Channel
Publisher: pub.php
$redis = new Redis ();
The first parameter is the IP of the Redis server, and the second port is the one
$res = $redis->connect (' 127.0.0.1 ', 6379);
Auth
$redis->auth (' 123456 ');
$channel = $argv [1]; Channel
$msg = $ARGV [2]; Msg
$redis->publish (' channel ' $channel, $msg);
How to use: (Command-line operation)
PHP pub.php test ' msg '
Note: Test sends a message to a subscription channel,msg
Process:
Subscription, after launch, will listen and respond in real time
Release
Other languages, such as Java,python, refer to: http://blog.csdn.net/freebird_lb/article/details/7778959,http://guozhiwei.iteye.com/blog/1240600
Redis Series (three)--subscribe/Publish