jmason81's avatar

Laravel Livewire + Pusher - no response client side

I'm able to broadcast a notification to Pusher, but I'm unable to receive the response back in my livewire component.

Here is my Notification class:

<?php

namespace App\Notifications;

use App\Models\Statement;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class StatementCompletedNotification extends Notification implements ShouldQueue, ShouldBroadcast
{
    use Queueable;

    public $statement;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(Statement $statement)
    {
        $this->statement = $statement;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return ['database', 'broadcast'];
    }

    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            'user_id' => $this->statement->uploadedBy->id,
            'statement_id' => $this->statement->id,
            'file_name' => $this->statement->original_file_name
        ];
    }


    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('users.' . $this->statement->uploadedBy->id);
    }
}

And here is the getListeners() method on my Livewire component. I've tried several different things here, first off I tried the way it's shown in the docs, just by referencing my StatementCompletedNotification in the listener, like so:

    public function getListeners()
    {
        return [
            "echo-private:users.{$this->user->id},StatementCompletedNotification" => 'refreshNotifications'
        ];
    }

I noticed that in pusher, my event type is listed as Illuminate\Notifications\Events\BroadcastNotificationCreated, and I found this post online, so I tried that method like so:

    public function getListeners()
    {
        return [
            "echo-private:users.{$this->user->id},.Illuminate\Notifications\Events\BroadcastNotificationCreated" => 'refreshNotifications'
        ];
    }

Neither way has worked for me.

Here's where I'm attempting to just get something back in my javascript on the client-side:

    Echo.private('users.1')
        .notification((notification) => {
			console.log(notification);
		});

I don't get any response and I have no idea what the problem is. I've spent an insane amount of time on this and can't seem to figure it out.

0 likes
2 replies
leonnicklas's avatar

Found my problem... For me it was just that I moved the Event itself inside a folder and didn't update the path of the Event file in the livewire getListeners() array...

My event file is now under app/Events/Subfolder/Event.php so my listener needed to be updated from this:


    protected function getListeners(): array
    {
        return [
            'echo-private:requests.'.Auth::id().',UpdatedEvent' => 'UpdatedEvent',
        ];
    }

to:


    protected function getListeners(): array
    {
        return [
            'echo-private:requests.'.Auth::id().',Subfolder\UpdatedEvent' => 'UpdatedEvent',
        ];
    }

Stupidly simple but maybe somebody finds it helpful too;)

Please or to participate in this conversation.