2

I'm trying to understand how to catch an event backend side once a user joined the channel. I mean right after the authentication has been done for a Private or a Presence channel.

For instance, after that:

Broadcast::channel('chat.{roomId}', function ($user, $roomId) {
    if ($user->canJoinRoom($roomId)) {
        return ['id' => $user->id, 'name' => $user->name];
    }
});

successfully proceeds, I would expect to have the possibility to catch an event to know that the user joined the channel (not only frontend side).

Any hint on how to handle it?

Reference: https://laravel.com/docs/6.x/broadcasting#joining-presence-channels

Giuseppe87
  • 406
  • 4
  • 14

1 Answers1

1

To handle the event on the backend, the client needs to communicate when they join a channel.

axios.post('/channel/join')
     .then(res => res.data)
     .then(data => console.log);

On the backend, you can have a route in (e.g.) routes/web.php:

Route::post('/channel/join', 'ChannelController@joined');

Then, in ChannelController.php your can process the action and do what you want:

public function joined(Request $request)
{
    $user = auth()->user();

    broadcast(new UserJoinedChannel($user))->toOthers();

    return ['status' => 'Channel joined successfully!'];
}
Luca Puddu
  • 336
  • 1
  • 11
  • We finally found the solution by using the pusher events: https://pusher.com/docs/channels/using_channels/events/ – Giuseppe87 Mar 03 '23 at 17:20