首页 > 解决方案 > Laravel Websockets 没有提供更新或者 Laravel Echo 没有监听它们,有时

问题描述

一年半以来,我一直在努力寻找解决方案,这是我关于这个问题的另一篇帖子,但目前还没有解决方案https://github.com/beyondcode/laravel-websockets/issues/322

由于那篇文章没有任何改变,我拥有我所涉及的所有资源的最新版本(上个半月更新)

包.json

"laravel-echo": "^1.10.0",
"laravel-echo-server": "^1.6.2",

作曲家.json

"beyondcode/laravel-websockets": "^1.11.1",
"pusher/pusher-php-server": "^4.1.5"

并且由于该问题报告没有任何改变,现在不同的是我的问题在聊天模块中更为持久。

基本上,websockets 在大多数情况下都可以工作,但是由于某些奇怪的原因,在某些时候决定不将更新传递给订阅的用户,或者 laravel echo 进入睡眠状态并忘记处理传递的更新,所以人 X 发送了一条消息给 Y 人,但 Y 人不会收到所述消息,在某个时候,当 X 人发送一条新消息时系统再次开始工作,然后 Y 人突然收到所有消息,但第一条消息最初是在 30 分钟前发送的太晚了,另一种情况是人们甚至可以在他们的聊天中看到重复的消息,尽管这并不让我太担心未交付的更新问题,因为我的应用程序可以用来通知用户人们准备好了预约服务台,而那些用户不会不知道,因为消息从未及时传递。

这是偶尔发生的事情,以至于我无法在本地环境中重现它,因此我可以轻松跟踪问题。系统 90% 的时间都在工作,但其他 10% 的时间似乎对我的用户的影响足以代表他们遇到的问题。如果您按照 github 链接,从那时起该代码没有任何变化,但我将添加聊天模块订阅的代码(请记住,我有其他 vue 文件具有对其他模块的不同订阅(我认为有 5 个不同每个用户的频道订阅总数):

对话.vue

/**
 * Starts listening to update conversation events.
 *
 */
updateChat() {
    Echo.channel(`update-conversation.${this.office_id}.${this.conversation.id}.${this.user_id}`)
        .listen('.list.new.messages', (event) => {
            this.listNewMessages();
        });
},

/**
 * Stops listening to update conversation events.
 *
 */
stopUpdatingChat() {
    Echo.leaveChannel(`update-conversation.${this.office_id}.${this.conversation.id}.${this.user_id}`);
    Event.$emit('stop-new-devsupport-conversation', this.conversation.id, this.user_id);
},

频道.php

Broadcast::channel('update-conversation.{officeId}.{conversationId}.{userId}', function ($user, $officeId, $conversationId, $userId) {
    return ($user->hasPermission('chat') && $user->office_id === $officeId && $user->id === $conversationId && User::find($userId)->office_id === $officeId);
});

这是事件ConversationEvent.php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

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

    public $officeId;
    public $conversationId;
    public $userId;

    /**
     * Create a new event instance.
     *
     * @param  int  $officeId
     * @param  int  $conversationId
     * @param  int  $userId
     * @return void
     */
    public function __construct($officeId, $conversationId, $userId)
    {
        $this->officeId = $officeId;
        $this->conversationId = $conversationId;
        $this->userId = $userId;
    }

    /**
     * The event's broadcast name.
     *
     * @return string
     */
    public function broadcastAs()
    {
        return 'list.new.messages';
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('update-conversation.' . $this->officeId . '.' . $this->conversationId . '.' . $this->userId);
    }
}

除此之外,这是Bootstrap.js文件中的Echo配置

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: window.PUSHER_APP_KEY,
    wsHost: window.location.hostname,
    wsPort: window.APP_DEBUG ? 6001 : 6002,
    wssPort: window.APP_DEBUG ? 6001 : 6002,
    disableStats: true,
    encrypted: !window.APP_DEBUG,
    enabledTransports: ['ws', 'wss'],
    forceTLS: false,
});

广播.php

'connections' => [
    'pusher' => [
        'driver' => 'pusher',
        'key' => env('PUSHER_APP_KEY'),
        'secret' => env('PUSHER_APP_SECRET'),
        'app_id' => env('PUSHER_APP_ID'),
        'options' => [
            'cluster' => env('PUSHER_APP_CLUSTER'),
            'encrypted' => true,
            'host' => '127.0.0.1',
            'port' => 6001,
            'scheme' => 'http',
        ],
    ],
]

就像我在我的 github 帖子上所说的那样,我完全基于这个https://alex.bouma.dev/installing-laravel-websockets-on-forge/来配置我的 websockets 以使其与 Forge 一起使用

我不知道,但是如果用户在一定时间后没有收到更新,自动取消订阅频道可能会超时吗?我真的不这么认为,因为一些客户发给我的屏幕截图在消息之间有 8 分钟的间隔。

我真的,真的很想最终找到解决这个问题的方法,所以请,如果有人可以伸出援手,我将非常感激。

提前致谢。

标签: laravelwebsocketpusherlaravel-echolaravel-websockets

解决方案


推荐阅读