0

I've moved my Laravel Events into subdirectories, and now my broadcasted messages are not being received. Pusher shows them ok, and if I move them out of the subdirectories then they work as expected, so I believe this is a namespacing issue, but I cannot figure out how to make it work.

The Event I'm focusing on is called TeamInvitationEvent and is located in the Events->Company->Memberships directory.

Here is my Listener:

public function getListeners()
{
    $user_id = Auth::id();
    
    return [
        'refresh-navigation-top-menu' => '$refresh',
        "echo-private:user.{$user_id},TeamInvitationEvent" => 'notifyInvitation',
        "echo-private:user.{$user_id},TeamInvitationCancelledEvent" => 'notifyInvitationCancelled',
        "echo-private:user.{$user_id},TeamInvitationAcceptedEvent" => 'notifyInvitationAccepted',
        "echo-private:user.{$user_id},TeamMemberRemovedEvent" => 'notifyMemberRemoved',
    ];
}

Here is a screenshot of my Pusher debug console, showing that the user is subscribed to the correct private channel, and that the API Message is being broadcast on that channel:

Pusher Screenshot

Here is a list of different ways I've tried to correct for the new namespace:

  • App.Events.Company.Memberships.TeamInvitationEvent
  • App\Events\Company\Memberships\TeamInvitationEvent
  • /App/Events/Company/Memberships/TeamInvitationEvent
  • .TeamInvitationEvent

There are no errors in the Laravel logs, or in the Pusher logs, and as I mentioned it works as expected if I don't move them into the subdirectory.

I'm running the following in my app:

  • Laravel v8
  • Livewire v2
  • Laravel Echo v1
  • Pusher
Andrew Fox
  • 794
  • 4
  • 13
  • 30

1 Answers1

2

This was embarrassingly obvious, but here's what fixed it:

"echo-private:user.{$user_id},Company\Memberships\TeamInvitationEvent" => 'notifyInvitation'

The docs are very clear that "App\Events" is prepended to all events, so naturally I just needed to add the rest of the namespace to get it working. I looked back and I had actually tried this earlier, but I must have forgot to clear the cache. Big shout-out to @joshhanley on the Livewire Discord chat for getting me sorted on this.

Side Note: using broadcastAs() made this much cleaner:

public function broadcastAs()
{
    return 'team.invitation';
}

But don't forget to add a '.' before this custom name in your view, like this:

"echo-private:user.{$user_id},.team.invitation" => 'notifyInvitation'
Andrew Fox
  • 794
  • 4
  • 13
  • 30
  • 1
    Don't feel embarrassed. I spent nearly an hour trying to figure this out... Glad it was an easy fix though – Chris Jan 19 '23 at 21:59