【问题标题】:Broadcasting, laravel-echo-server not receiving events广播,laravel-echo-server 未接收事件
【发布时间】:2018-03-08 13:30:54
【问题描述】:

我在一个项目中使用 Laravel,我想通过 laravel-echo-server 和 Redis 使用广播。我已经在 docker 容器中进行了设置。下面的输出:

Redis

    redis_1                | 1:C 27 Sep 06:24:35.521 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
    redis_1                | 1:C 27 Sep 06:24:35.577 # Redis version=4.0.2, bits=64, commit=00000000, modified=0, pid=1, just started
    redis_1                | 1:C 27 Sep 06:24:35.577 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
    redis_1                | 1:M 27 Sep 06:24:35.635 * Running mode=standalone, port=6379.
    redis_1                | 1:M 27 Sep 06:24:35.635 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
    redis_1                | 1:M 27 Sep 06:24:35.635 # Server initialized
    redis_1                | 1:M 27 Sep 06:24:35.635 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
    redis_1                | 1:M 27 Sep 06:24:35.636 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
    redis_1                | 1:M 27 Sep 06:24:35.715 * DB loaded from disk: 0.079 seconds
    redis_1                | 1:M 27 Sep 06:24:35.715 * Ready to accept connections

一些警告,但没有破坏。

laravel-echo-server

laravel-echo-server_1  | L A R A V E L  E C H O  S E R V E R
laravel-echo-server_1  | 
laravel-echo-server_1  | version 1.3.1
laravel-echo-server_1  | 
laravel-echo-server_1  | ⚠ Starting server in DEV mode...
laravel-echo-server_1  | 
laravel-echo-server_1  | ✔  Running at localhost on port 6001
laravel-echo-server_1  | ✔  Channels are ready.
laravel-echo-server_1  | ✔  Listening for http events...
laravel-echo-server_1  | ✔  Listening for redis events...
laravel-echo-server_1  | 
laravel-echo-server_1  | Server ready!
laravel-echo-server_1  | 
laravel-echo-server_1  | [6:29:38 AM] - dG0sLqG9Aa9oVVePAAAA joined channel: office-dashboard

客户端加入频道似乎没有任何问题。 但是,如果我启动一个事件 laravel-echo-server 不会收到该事件。

我做了一些研究,发现了一些关于队列工作者的信息。所以我决定运行它(php artisan queue:work),看看它是否有任何作用。根据文档,它应该只运行队列中的第一个任务然后退出(而不是queue:listen)。果然它开始处理我之前踢的事件。但它并没有停止并继续前进,直到我杀死它:

[2017-09-27 08:33:51] Processing: App\Events\CompanyUpdated
[2017-09-27 08:33:51] Processing: App\Events\CompanyUpdated
[2017-09-27 08:33:51] Processing: App\Events\CompanyUpdated
[2017-09-27 08:33:51] Processing: App\Events\CompanyUpdated
etc..

redis 容器中显示以下输出:

redis_1                | 1:M 27 Sep 06:39:01.562 * 10000 changes in 60 
seconds. Saving...
redis_1                | 1:M 27 Sep 06:39:01.562 * Background saving started by pid 19
redis_1                | 19:C 27 Sep 06:39:01.662 * DB saved on disk
redis_1                | 19:C 27 Sep 06:39:01.663 * RDB: 2 MB of memory used by copy-on-write
redis_1                | 1:M 27 Sep 06:39:01.762 * Background saving terminated with success

现在我要么执行了太多 api 调用,导致队列如此庞大,要么出现了问题。此外,在“处理”作业后,laravel-echo-server 没有显示任何输出。

我在我的模型中创建了一个钩子来触发事件:

public function __construct(array $attributes = []) {
    parent::__construct($attributes);

    parent::created(function( $model ){
        //event(new CompanyCreated($model));
    });

    parent::updated(function( $model ){
        event(new CompanyUpdated($model));
    });

    parent::deleted(function( $model ){
        event(new CompanyDeleted($model));
    });
}

那么这就是它开始的事件:

class CompanyUpdated implements ShouldBroadcast {
use Dispatchable, InteractsWithSockets, SerializesModels;

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

/**
 * Get the channels the event should broadcast on.
 *
 * @return Channel|array
 */
public function broadcastOn() {
    return new Channel('office-dashboard');
}
}

最后,这是前端监听事件的代码:

window.Echo.channel('office-dashboard')
  .listen('CompanyUpdated', (e) => {
    console.log(e.company.name);
  });

.env文件:

BROADCAST_DRIVER=redis
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=redis

REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379

为什么事件没有传递给 laravel-echo-server?有什么我遗漏或忘记的吗?

【问题讨论】:

  • 你是否在 .env 中更改了广播驱动程序?
  • 是的,我已经更新了我的问题以显示我的 .env 中的相关代码
  • 你有没有排队:工作进程?似乎它失败了。也许您可以尝试将队列驱动程序更改为首先同步以查看错误,如果它没有处理队列
  • 你配置 echo 连接服务器了吗?
  • 抱歉回复晚了,但我使用了 docker 容器。他们应该注意连接。无论如何,当我这周继续工作时,它开始出人意料地工作,我什至没有改变任何东西。无论如何,谢谢。

标签: php laravel broadcast


【解决方案1】:

突然开始工作。

【讨论】:

  • 你再也没有遇到过同样的问题吗?我认为这是因为当您致电 window.Echo.channel().listen() 时,Echo 还没有准备好订阅。我遇到了同样的问题,所以我查看了 chrome 开发工具的 websocket 流。 Websocket 已成功连接,但订阅消息未发送到 laravel-echo-server。所以我使用setTimeout 等待 1000 毫秒,然后一切正常。什么错误
【解决方案2】:

对我来说,@jesuisgenial 的评论为我指明了正确的方向。

您可以通过检查 Chrome 开发人员工具中 Networking 选项卡下的 Websockets 选项卡轻松识别它是否未订阅

无 1 秒延迟(无订阅事件):

Echo.private(`users.${context.getters.getUserId}`)
    .listen('.conversations.new_message', function(data) {
        console.log(data.message);
    })

延迟 1 秒(存在订阅事件):

setTimeout(() => {
    Echo.private(`users.${context.getters.getUserId}`)
        .listen('.conversations.new_message', function(data) {
            console.log(data.message);
        })
}, 1000)

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 2020-07-28
    • 2020-02-18
    • 1970-01-01
    • 2021-05-06
    • 2023-03-24
    • 1970-01-01
    • 2019-09-14
    相关资源
    最近更新 更多