我正在尝试构建一个聊天应用程序,为此,我使用的是Laravel的Echo和Laravel echo服务器。想法是听取消息通知。
我按照以下方式做了一切:
https://laravel.com/docs/master/notifications
https://laravel.com/docs/master/broadcasting
resources / assets / js / bootstrap.js:(当然我已经使用NPM将其删除了)
import Echo from 'laravel-echo'
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001'
});
laravel回波-server.json:
{
"authHost": "http://laravel-notifications.local",
"authEndpoint": "/broadcasting/auth",
"clients": [],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": ""
}
ChatController.php (触发通知)
$toUser = User::find($toUser)->notify(new MessageNotification($message));
MessageNotification.php
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'message' => $notifiable,
]);
}
刀片视图(收听通知):
window.Echo.private('App.User.' + window.Laravel.user.id)
.notification((notification) => {
console.log(notification); // Not getting executing
});
当我访问该页面时,我从Laravel Echo Server看到以下日志:
[上午9:34:41] - XnmH1wzMJM-5VON-AAAA认证:private-App.User.7
[上午9:34:41] - XnmH1wzMJM-5VON-AAAA加入频道:private-App.User.7
但是当我发出通知时,我在浏览器控制台上看不到任何内容。
完整源代码:
https://github.com/xparthxvorax/Laravel-Notifications
NB:我已成功收听该活动但未收到通知。
答案 0 :(得分:0)
在通知中,在广播频道部分中,您需要指定将分派广播的队列。例如:
public function toBroadcast($notifiable)
{
return (new BroadcastMessage([
'html' => $data
]))->onConnection('database')->onQueue('alerts');
}