Thank you for your blog

Before writing this article, I have completed the practice, Want to know moreredis Publish and subscribe mode and practical application of, This model is for producers and consumers, May be1-1 Relationship( For message queuing), Can also be1-N Relationship, It is said that the applicable scenarios are those with low requirements for data reliability, Log. The small function of my practice is based onWebSockets Of this two-way communication protocol, Mainly to solve the problem that the server actively pushes information to the client, Instead of polling, thisdemo Ofgithub address
It's based onPHP Third party package created "cboden/ratchet", Interested students can have a look, Their official website is
redis Server
windows end:
linux: <>

windows Download extensionsphp_redis.dll:

php Application of, Mustphp_redis extend
The following practices arewin10 system

First, decompressionredis Server package for, Direct double click‘redis-server.exe’, start-upredis service, Do not turn off the terminal that starts the pop-up, If the terminal is connected, it is closed againredis.
Same double click“redis-cli.exe” inputsubscribe first , Subscribed Channels
Run separately sub.php,subscribe The customer service terminal is blocked, You can run this on the terminalphp Code, The effect is more obvious; Or keep it running in the background
nohup /usr/bin/php /path/sub.php >> /tmp/sub.log 2>&1 &
Re run pub.php,
When executedpub.php after, stay redis-cli Terminal andsub.php Will receive“hello world!” Success. Principles need to be thought about.
#subscribe Customer service terminal sub.php $redis = new \Redis(); $res = $redis->pconnect(
'', 6379); $key = 'first'; # Theresubscribe Is an exclusive link, Your execution name at the terminal“subscribe
first” after, The screen will turn on as ifredis The terminal of the service is the same, Stay online and wait. So you can see the subscribers here( Customer service terminal) The program is waiting in a blocked way, Publisher's message.
$redis->subscribe(array($key),'callback'); function callback($redis, $channel,
$msg){ var_dump($redis); echo $channel; echo $msg; return true; } #public Customer service terminal
pub.php # No exclusive link required, It's not clogged $redis = new \Redis(); $res = $redis->connect(
'', 6379, 1 ); $key = 'first';//Channel Subscribers to this channel, You can receive messages $value =
'hello world!'; $res = $redis->publish($key,$value); We can finish a section here.
Here's a question, Large scale requestspub.php file, Request message, In order to resist high concurrent Publishing, Should queue be used again(redis), But we put all the newsredis In the queue, How to monitor in real timeredis Data in, I think most of them will, Is inlinux Timers often used in. as:
## Add a timer, Modify letpub.php Every minuteredis Intermediate data crontab -e */1 * * * * /path/pub.php
But is there a smarter one? For example, monitoring.redis Message queue for? If there's a direct notification. A little dizzy.....
Last point, This andratchet The difference is,ratchet Will start a service on the server itself, In this way, when the network is disconnected, Also push messages to customer service.