Laravel Notification not broadcasting without any error?

Viewed 1516

I'm trying to implement a notification system with Laravel and Vue JS. I implemented everything and it needs to be work but Notification class is not broadcasting.

.env file

BROADCAST_DRIVER=pusher
PUSHER_APP_ID=40xxxx
PUSHER_KEY=5a6axxxxxx
PUSHER_SECRET=b35xxxxxx

Already run those commands below:

npm install --save laravel-echo pusher-js
composer require pusher/pusher-php-server "~2.6"

in app.js (uncommented)

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

No errors in Laravel log. In the parallel, I am checking on Pusher's Debug Console only connect and disconnect requests on there. Why it's not pushing any kind of error and why it's not working properly?

2 Answers

You will also need to configure and run a queue worker. All event broadcasting is done via queued jobs so that the response time of your application is not seriously affected by events being broadcast.

Read more in docs https://laravel.com/docs/9.x/broadcasting#queue-configuration

It might be due to because all broadcasting is done via queued jobs so run queue work command

php artisan queue:work

If you found this helpful so please vote : ) happy codding

I had the same problem. In my case, the cause of the problem was the placement of the User model by namespace App\Models\User. The first way to solve this problem is to:

in BroadcastServiceProvider.php or routes/channels.php (if laravel 5.7+)

Broadcast::channel('App.Models.User.*', function ($user, $userID) {
    return (int) $user->id === (int) $userID;
});

The second way to solve this problem is:

In User model add method

public function receivesBroadcastNotificationsOn() {
    return 'users.'.$this->id;
}

and in BroadcastServiceProvider.php or routes/channels.php (if laravel 5.7+)

Broadcast::channel('users.{user_id}', function ($user, $id) {
    return (int) $user->id === (int) $id;
});
Related