如何使用流明广播将消息发布到redis频道?

时间:2017-10-19 05:46:09

标签: php laravel redis lumen

我有一个名为' Event1'和一个事件监听器' Event1Listener'我的流明应用程序中的事件。我需要将自定义消息发布到名为' channel1'的redis频道。何时触发event1。我怎么能这样做?

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?";
        }
    }
?>

1 个答案:

答案 0 :(得分:0)

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

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

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