[英]Not able to receive notification - Laravel with Socket.io
我正在嘗試構建一個聊天應用程序,為此,我正在使用Laravel的Echo和Laravel回顯服務器。 想法是聽消息通知。
我已經根據以下內容完成了所有操作:
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 AM]-XnmH1wzMJM-5VON-AAAA已針對以下用戶進行身份驗證:private-App.User.7
[9:34:41 AM]-XnmH1wzMJM-5VON-AAAA加入了頻道:private-App.User.7
但是,當我發出該通知時,我在瀏覽器控制台上看不到任何記錄。
完整的源代碼:
https://github.com/xparthxvorax/Laravel-Notifications
注意:我可以成功收聽事件,但不能收聽通知。
在通知中,在廣播頻道部分,您需要指定將調度廣播的隊列。 例如:
public function toBroadcast($notifiable)
{
return (new BroadcastMessage([
'html' => $data
]))->onConnection('database')->onQueue('alerts');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.