PHP code example of hollisho / yii2-redis-pub-sub

1. Go to this page and download the library: Download hollisho/yii2-redis-pub-sub library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hollisho / yii2-redis-pub-sub example snippets


// 前台发送
/* @var $redisPubSub RedisPubSub */
$redisPubSub = \Yii::$app->redisPubSub;
$redisPubSub->publish('test', 'xxxxxxxxxxxxxxx');

// console里面监听,并且处理,设置监听不超时
/* @var $redisPubSub RedisPubSub */
$redisPubSub = \Yii::$app->redisPubSub;
$redisPubSub->setOptReadTimeout(-1);
$redisPubSub->subscribe('test', function($instance, $channelName, $message) {
    var_dump($message);
});
 php
'redisPubSub'=>[
    'class' => 'hollisho\redis_pub_sub\RedisPubSub',
    'connect' => [
        'class' => 'yii\redis\Connection',
        'hostname' => '127.0.0.1',
        'port' => 6379,
        'database' => 0,
        'password' => '',
        'connectionTimeout' => 20,
    ]
],