Demers94
2 years ago

Laravel Echo not receiving broadcasted notifications via Pusher

Posted 2 years ago by Demers94

Hi everyone,

I'm using Pusher and Laravel Echo to broadcast notifications (not events) to the front-end. The notifications is sent to Pusher (I can see the graph update in real-time as I send the notifications) and also appears in the database, but for some reason I can't receive it on the front-end using Echo.

Here's my bootstrap.js file :

import Echo from 'laravel-echo'

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: 'my-key-here'
});

Here is the notification :

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class PostInFollowedTopic extends Notification
{
    use Queueable;

    public function via($notifiable)
    {
        return ['database', 'broadcast'];
    }

    public function toBroadcast($notifiable)
    {
        return new BroadcastMessage([
            'message' => 'Demo'
        ]);
    }
}

(I only kept the relevant code, I removed the constructor, toArray method, etc)

Here is how I'm trying to receive the notifications on the front-end :

Echo.private('App.User.1')
    .notification((notification) => {
        console.log(notification.type);
    });

I did uncomment the BroadcastServiceProvider in config/app.php :

App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,

I don't see any error in the console or in my application when I throw the notifications :

Route::get('/test', function(){
    Illuminate\Support\Facades\Auth::user()->notify(new App\Notifications\PostInFollowedTopic(App\Topic::first()));
});

The notification does show up in the database, so I'm assuming that there's no issue with this call.

Am I missing anything? Since it's showing up on the Pusher dashboard but not in my application, I'm assuming that there's something wrong with the way I setup Echo but I can't figure out the issue.

Thanks for the help!

Please sign in or create an account to participate in this conversation.