Thank you for your blog
https://www.cnblogs.com/sjfgod/p/7670703.html
<https://www.cnblogs.com/sjfgod/p/7670703.html>
https://www.cnblogs.com/qianlizeguo/articles/6856813.html
<https://www.cnblogs.com/qianlizeguo/articles/6856813.html>
http://blog.sina.com.cn/s/blog_62b832910100xok2.html
<http://blog.sina.com.cn/s/blog_62b832910100xok2.html>

Before writing this article , I have completed the practice , Want to know more redis Publish and subscribe mode and practical application of , This model is for producers and consumers , Can be 1-1 Relationship of ( For message queuing ), Or 1-N Relationship of , It is said that the applicable scenarios are those with low requirements for data reliability , Such as log . The small function of my practice is based on WebSockets Of this two-way communication protocol , Mainly to solve the problem that the server actively pushes information to the client , Instead of polling , this demo Of github address https://github.com/titlebaby/even-Communications.
It's based on PHP Third party package created "cboden/ratchet", Interested students can have a look , Their official website is http://socketo.me/
redis Server for
windows end :https://github.com/mythz/redis-windows
<https://github.com/mythz/redis-windows>
linux:https://redis.io/download <https://redis.io/download>

windows Download extension php_redis.dll:
https://pecl.php.net/package/redis/3.1.6/windows
<https://pecl.php.net/package/redis/3.1.6/windows>

php Application of , Must be php_redis extend
The following practices are win10 system

first , decompression redis Server package for , Double click directly ‘redis-server.exe’, start-up redis service , Do not turn off the terminal that starts the pop-up , If the terminal is connected, it is closed again redis.
Double click again “redis-cli.exe” input subscribe first , Subscribed Channels
Run separately sub.php,subscribe The customer service terminal is blocked , You can run this on the terminal php 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 &
Rerun pub.php,
When the execution is finished pub.php after , stay redis-cli Terminal and sub.php Will receive “hello world!” Success . Principles need to be thought about .
#subscribe Customer service terminal sub.php $redis = new \Redis(); $res = $redis->pconnect(
'192.168.6.33', 6379); $key = 'first'; # there subscribe Is an exclusive link , Your execution name at the terminal “subscribe
first” after , The screen will turn on as if redis 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(
'192.168.6.33', 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 requests pub.php file , Ask for a message , In order to resist high concurrent Publishing , Should queue be used again (redis), But we put all the news redis In the queue of , How to monitor in real time redis Data in , I think most of them will , It's in linux Timers often used in . as :
## Add a timer , Modify the pub.php Every minute redis Fetching data in 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 is the same as ratchet 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 .