【问题标题】:Laravel event broadcast not triggeringLaravel 事件广播未触发
【发布时间】:2016-07-20 15:57:03
【问题描述】:

我看不到为什么没有触发 broadcastOn()。这是我的代码:

    <?php

namespace App\Events;

use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class NotificationEvent extends Event implements ShouldBroadcast{
    use SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public $message;
    public $user;
    public function __construct($user_id,$message){
            $this->user = $user_id;
            $this->message = $message;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        //var_dump("Sending event to:".'userNotifications.'.$this->user_id);
        //return ['userNotifications.'.$this->user_id];
        \Log::info("broadcast notification");
        \Log::info($this->message);
        return ['notifications'];
    }
}

登录 _construct 有效,但登录 broadcastOn() 无效。

我的 .env 文件设置为 redis,Redis 服务器已开启并监听 3001 端口。

有谁知道为什么这现在可行?

【问题讨论】:

    标签: php laravel events redis


    【解决方案1】:

    实际的广播是一个 Job。查看你的 Laravel 作业队列。当作业通过php artisan queue:work 执行时,broadcastOn() 方法将触发。

    【讨论】:

    • 谢谢,从来没有意识到这一点。一个未更新的队列工作者让我头疼:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 2018-03-08
    • 2022-10-24
    相关资源
    最近更新 更多