【问题标题】:Laravel Echo private channel not listeningLaravel Echo 私人频道不听
【发布时间】:2020-01-15 03:10:36
【问题描述】:

我正在尝试创建一个实时聊天应用程序。我使用了 Laravel Echo、Redis 和 socket-io。

但是laravel-echo没有收听私聊聊天频道。

App\Events\MessagePushed

public function broadcastAs()
{
  return 'new.message';
}

public function broadcastOn()
{
  return new PrivateChannel("application-chat-{$this->message->job_application_id}");
}

routes/channels.php

Broadcast::channel('application-chat-{job_application_id}', function ($user, $job_application_id) {
    return auth()->check();
});

Store Message resource method

public function store(StoreFormRequest $request, JobApplication $application)
{
   $request->merge([
      'sender_id' => $request->user()->id,
   ]);

   $message = $application->messages()->create($request->all())
                          ->load(['sender', 'receiver']);

   event(new MessagePushed($message));

   return response()->json([
      'message' => $message,
   ]);
}

Listening to private channel

window.Echo.private(`application-chat-${this.messageApplication}`)
      .listen('.new.message', (data) => {
          console.clear();
         console.log('Got event...', data);
});

【问题讨论】:

  • 你试过直接指向事件吗? .listen('MessagePushed')
  • 我试过了,没有变化
  • 你使用什么作为套接字提供者?
  • 我使用 Redis 作为消息广播。
  • 如果我没记错的话.. 那很好,但是您仍然需要一些可以将这些发送给用户的东西。看看 Beyondcode/laravel-websockets & tlaverdure/laravel-echo-server

标签: laravel redis socket.io laravel-echo


【解决方案1】:

希望对调试有帮助:

Laravel 回显服务器配置:

{
    "authHost": "domain.local",
    "authEndpoint": "/broadcasting/auth",
    "database": "redis",
    "databaseConfig": {
        "redis": {}
    },
    "devMode": true,
    "host": null,
    "port": "6001",
    "protocol": "http",
    "socketio": {},
    "sslCertPath": "",
    "sslKeyPath": "",
    "sslCertChainPath": "",
    "sslPassphrase": "",
    "subscribers": {
        "http": true,
        "redis": true
    },
    "apiOriginAllow": {
        "allowCors": false,
        "allowOrigin": "",
        "allowMethods": "",
        "allowHeaders": ""
    }
}

php 工匠队列:工作:

检查队列是否发送事件

laravel 回显服务器

检查用户是否加入频道并回显服务器发送事件

Laravel Echo:

broadcastAs 可能有问题。尽量不要使用它。不要忘记将命名空间属性添加到配置中。

window.Echo = new Echo({
    namespace: 'App.Events',
    broadcaster: 'socket.io',
    host: window.location.protocol + '//' + window.location.hostname + ':6001',
    auth: {
        headers: {
            Authorization: //auth
        }
    }
});

事件名称 = 事件类名称

window.Echo.private(`application-chat-${this.messageApplication}`)
    .listen('MessagePushed', (data) => {
        console.log(data);
    });

【讨论】:

    【解决方案2】:

    我认为问题出在命名空间

    window.Echo = new Echo({
        namespace: 'App.Events',
    });
    

    【讨论】:

      猜你喜欢
      • 2019-11-05
      • 2020-02-13
      • 2020-11-22
      • 1970-01-01
      • 2020-12-21
      • 2019-06-04
      • 2019-02-08
      • 2017-05-12
      • 2021-03-09
      相关资源
      最近更新 更多