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

Hexagon-Dev's avatar

How to auth a user on event receive from Reverb?

I have a project with chat on Laravel 11, Reverb and Nuxt 3 with Pusher. I set up broadcasts in bootstap/app.php:

->withBroadcasting(
        __DIR__.'/../routes/channels.php',
        ['prefix' => 'api', 'middleware' => ['api', 'auth:sanctum']],
    )

Also I set up channel:

Broadcast::channel('chat.{id}', function ($user, $id) {
    $chat = \App\Models\Chat::query()->find($id);

    if (!$chat) {
        return false;
    }

    return $chat->users->contains($user);
});

Also I created ReverbEventListener which receives Laravel\Reverb\Events\MessageReceived in handle method. In this method I need to obtain user to understand who sent a request. In pusher lib I set auth endpoint - during testing it successfully authed and app connected to private channel - events are sending successfully, however on message receive - there is no user, I checked it via adding:

Log::debug('User', [auth('sanctum')->user()]);

What I am doing wrong? Or should I create private channel per chat per user?

0 likes
0 replies

Please or to participate in this conversation.