php - 如何使用 lumen 广播将消息发布到 redis channel ?

标签 php laravel redis lumen

我的 lumen 应用程序中有一个名为“Event1”的事件和一个事件监听器“Event1Listener”。当 event1 被触发时,我需要将自定义消息发布到名为“channel1”的 redis channel 。我该怎么做?

Event1.php

<?php
    namespace App\Events;
    use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
    class Event1 extends Event implements ShouldBroadcast
    {
        /**
        * Create a new event instance.
        * @return void
        */
        public function __construct() {        
        }
        /**
        * Get the channels the event should be broadcast on.
        *
        * @return array
        */
        public function broadcastOn() {
            return ['channel1'];
        }
    }
?>

Event1Listener.php

<?php

    namespace App\Listeners;

    use App\Events\Event1;
    use Illuminate\Queue\InteractsWithQueue;
    use Illuminate\Contracts\Queue\ShouldQueue;

    class Event1Listener  {
        /**
        * Create the event listener.
        *
        * @return void
        */
        public function __construct(){
        }
        /**
        * Handle the event.
        *
        * @param  Event1  $event
        * @return void
        */
        public function handle(Event1 $event) {
            echo "What should I add here?";
        }
    }
?>

最佳答案

假设您已经配置了 Redis 如果没有,请查看 laravel documentationlumen documentation

要将消息发布到 channel ,您可以使用命令

public function handle(Event1 $event) {
    Redis::publish('channel1', json_encode(['foo' => 'bar']));
}

关于php - 如何使用 lumen 广播将消息发布到 redis channel ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46823462/

相关文章:

php - 将 Metatag 字符串插入 mysql 表

php - 获取图像中所有可用的隐藏数据

php - Laravel 同步 与可选参数的关系

php - 点击链接后 JQuery 提交表单

php - Opencart:产品名称打印多次,如何解决。?

javascript - VueJS 总和数据透视表

php - laravel 如何找到模型与其在数据库中的表之间的连接

c++ - 在 Windows 上使用 hiredis 进行开发

binary - Redis 和转义二进制数据

redis - 是否可以重命名 AWS ElastiCache Redis 中的命令?