Tip:
redis publish and subscribe notification is based on socket, the connection timeout is affected by the configuration, you can modify php.ini , or add
ini_set('default_socket_timeout', -1);
dynamically
pub.php
``` $redis = new Redis(); // 第一个参数为redis服务器的ip,第二个为端口 $res = $redis->connect('127.0.0.1', 6379); // test为发布的频道名称,hello,world为发布的消息 $res = $redis->publish('test','hello,world'); ```
sub.php
``` $redis = new Redis(); $res = $redis->connect('127.0.0.1', 6379,0); $redis->subscribe(array('test'), 'callback'); // 回调函数,这里写处理逻辑 function callback($instance, $channelName, $message) { echo $channelName, "==>", $message,PHP_EOL; } ```
The above is the detailed content of PHP+Redis publish and subscribe. For more information, please follow other related articles on the PHP Chinese website!