我正在尝试用Laravel和Vue JS实现一个通知系统。我实现了所有的东西,它需要工作,但是Notification没有广播。
.env文件
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=40xxxx
PUSHER_KEY=5a6axxxxxx
PUSHER_SECRET=b35xxxxxx已经在下面运行这些命令:
npm install --save laravel-echo pusher-js
composer require pusher/pusher-php-server "~2.6"在app.js中(未注释)
App\Providers\BroadcastServiceProvider::class,BroadcastServiceProvider.php
Broadcast::channel('App.User.*', function ($user, $userID) {
return (int) $user->id === (int) $userID;
});bootstrap.js
import Echo from "laravel-echo"
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: '5a6axxxxxx',
cluster: 'eu',
encrypted: true,
authEndpoint: "/broadcasting/auth"
});
window.Pusher.log = function(message){
window.console.log(message);
}
window.Echo.private('App.User.1')
.notification((notification) => {
console.log(notification.type);
});在Laravel日志中没有错误。在并行环境中,我只检查there的调试控制台上的连接和断开请求。为什么它没有推动任何类型的错误,为什么它不能正常工作?
发布于 2022-03-30 11:59:34
您还需要配置和运行一个队列工作者。所有事件广播都是通过排队作业完成的,这样应用程序的响应时间就不会受到广播事件的严重影响。
在docs https://laravel.com/docs/9.x/broadcasting#queue-configuration中阅读更多内容
这可能是因为所有广播都是通过排队作业完成的,所以运行队列工作命令。
php artisan queue:work如果你觉得这很有帮助,请投票:)
发布于 2019-06-12 11:25:45
我也有同样的问题。在我的例子中,问题的原因是User模型被命名空间App\Models\User放置。解决这个问题的第一个方法是:
在BroadcastServiceProvider.php或routes/channels.php els.php中(如果是laravel 5.7+)
Broadcast::channel('App.Models.User.*', function ($user, $userID) {
return (int) $user->id === (int) $userID;
});解决这一问题的第二个办法是:
在User模型中添加方法
public function receivesBroadcastNotificationsOn() {
return 'users.'.$this->id;
}以及在BroadcastServiceProvider.php或routes/channels.php els.php中(如果是laravel 5.7+)
Broadcast::channel('users.{user_id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});https://stackoverflow.com/questions/46373961
复制相似问题