Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Event from public channel to private channel

Hello,

I have a problem with an event.

class MicroRequestsReceived implements ShouldBroadcastNow

...

public function broadcastOn(): array
{
    return [
        // new PrivateChannel('users.'.$this->body->user_id),
        new Channel('micros.requests'),
    ];
}

public function broadcastAs(): string
{
    return 'micro.requests.received';
}

And I listen to this event from VueJS.

websocketStore.channel.bind('micro.requests.received', async (data) => {
    log('Requests list received from a micro-service');

    globalStore.addMicroRequests(data.body);
});

All the other events work fine with a private channel.

This event was with a public channel until today, I need to use a private channel for this event now.

But it doesn't work.

  • public channel : I get the informations in VueJS

  • private channel : I don't get the informations, the event is triggered, but not listened to by VueJS

Why do you suggest me to check ?

Thanks for your help.

V

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

Certainly! When switching from a public channel (Channel) to a private channel (PrivateChannel) in Laravel broadcasting, there are a few important things to check to ensure your event is received on the frontend:


1. Update Your Event to Use a Private Channel

Change your event's broadcastOn method to use PrivateChannel:

public function broadcastOn(): array
{
    return [
        new PrivateChannel('users.' . $this->body->user_id),
    ];
}

2. Update Your Frontend Listener

When listening to private channels in Laravel Echo (or Pusher JS), you must use .private() instead of .channel() or .listen():

Example using Laravel Echo:

Echo.private('users.' + userId)
    .listen('.micro.requests.received', (data) => {
        log('Requests list received from a micro-service');
        globalStore.addMicroRequests(data.body);
    });

If using Pusher JS directly:

websocketStore.pusher.subscribe('private-users.' + userId)
    .bind('micro.requests.received', (data) => {
        log('Requests list received from a micro-service');
        globalStore.addMicroRequests(data.body);
    });

3. Ensure Authentication Endpoint is Set Up

Private channels require authentication. Make sure your /broadcasting/auth route is accessible and returns the correct response. Laravel provides this out of the box if you use BroadcastServiceProvider.


4. Check User Authorization

In your routes/channels.php, you must authorize users to listen to the private channel:

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

5. Debugging Tips

  • Open your browser's network tab and look for requests to /broadcasting/auth. Ensure they return 200 OK.
  • Check for any JavaScript errors in the console.
  • Ensure the user is authenticated in your frontend app before subscribing to the private channel.

Summary

Switching to a private channel requires:

  • Using PrivateChannel in your event.
  • Listening with .private() in the frontend.
  • Ensuring the user is authenticated and authorized to listen to the channel.

If you follow these steps, your event should be received on the frontend just like with the public channel, but now securely via a private channel.

Let me know if you need more specific code examples for your setup!

1 like

Please or to participate in this conversation.