Search code examples
laravelechopusherlaravel-echo

Laravel echo doesn't listen to private notification callback. Tried everything on the internet


I've been doing debugging my laravel Echo.

Here is what I have in my bootstrap.js. I am not using vue.js and I just using local server localhost:8000.

import EchoObj from 'laravel-echo';

window.Echo = new EchoObj({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    encrypted: true
});

Echo.private('App.User.'+ loggedUser.id)
.notification((notification) => {
    console.log(notification.type);
    alert("listening")
})

My NotificationEvent controller:

Class NotificationEvent extends Notification{
    public function via($notifiable)
    {
        return ['database', 'broadcast', 'mail'];
    }

   public function toBroadcast($notifiable){
        return new BroadcastMessage([
            "test" => "nesting"
        ]);
    }

    public function toArray($notifiable){}
    public function toDatabase($notifiable){}
}

Channel.php

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

I know why my echo is not working because I've checked the pushed debug console whenever I fire an event like event(new NotificationEvent) i get this message on pusher

{
  "test" : "nesting"
  "id"  : "ecc9912f-0ae2-44dc-b905-677f93bfc38b"
  "type" : "App\\Notifications\\WorkflowUpdateNotification"
}

My public channels are working fine though. What is missing on my private channel.


Solution

  • Solution

    For some reason my channel on broadcast was not working for the private channel. so, I had to specify exactly which channel notification to receive. So, I update the User Model like this and it worked.

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