PHP+Redis publish and subscribe

藏色散人
Release: 2023-04-07 13:24:01
forward
3515 people have browsed it

PHP+Redis publish and subscribe

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);
Copy after login

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');
```
Copy after login

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;
}
```
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!