Laravel Echo私人频道未收听

时间:2019-09-13 08:50:45

标签: laravel redis socket.io laravel-echo

我正在尝试创建一个实时聊天应用程序。我使用了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);
});

2 个答案:

答案 0 :(得分: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 artisan queue:work:

检查队列是否发送事件

enter image description here

laravel回显服务器

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

enter image description here

Laravel回声:

可能是broacastAs问题。尝试不使用它。不要忘记将名称空间属性添加到配置中。

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);
    });

答案 1 :(得分:0)

我认为问题出在命名空间上

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