#Getting the number of users in a Laravel Reverb presence channel

6 messages · Page 1 of 1 (latest)

errant glade
#

Hello,

I am building a Looking For Group app that has a "rooms" system for each game utilising Laravel Reverb, a user can create a room and set how many slots the room has.

I was wondering if there is any way to check how many users are in a presence channel with Reverb to deny them access to the room if it's full, or if there is a way for me to listen to some server-side disconnect event on that room's channel because that way I could store a new room member in a database in the channels.php callback function and remove them when the event fires.

Broadcast::channel("room.{gamePath}.{roomId}", function(User $user) {
    // TODO: check for room space

    return [
        'provider_id' => $user->provider_id,
        'username' => $user->username,
        'avatar' => $user->avatar,
        'is_verified_creator' => $user->is_verified_creator
    ];
});

random jungle
errant glade
#

Gotcha, and app_id is the REVERB_APP_ID in my .env file I'm assuming?

random jungle
#

Correct

errant glade
#

Makes sense, thank you peepolove

errant glade
#

For anyone stumbling onto this thread, this is how I managed to get the amount of users in a presence channel:

Broadcast::channel("room.{gamePath}.{roomId}", function(User $user, string $gamePath, string $roomId) {
    $REVERB_APP_ID = env("REVERB_APP_ID");
    $REVERB_HOST = env("REVERB_HOST");
    $REVERB_PORT = env("REVERB_PORT");
    $REVERB_SCHEME = env("REVERB_SCHEME");
    $REVERB_APP_KEY = env("REVERB_APP_KEY");
    $REVERB_APP_SECRET = env("REVERB_APP_SECRET");

    $timestamp = time();

    $url = "$REVERB_SCHEME://$REVERB_HOST:$REVERB_PORT/apps/$REVERB_APP_ID";
    $endpoint = "/channels/presence-room.$gamePath.$roomId";
    $parameters = "auth_key=$REVERB_APP_KEY&auth_timestamp=$timestamp&auth_version=1.0&info=user_count";

    $stringToSign = "GET\n/apps/$REVERB_APP_ID" . "$endpoint\n$parameters";
    $authSignature = hash_hmac("sha256", $stringToSign, $REVERB_APP_SECRET);

    $authUrl = "?$parameters&auth_signature=$authSignature";

    $response = Http::get($url . $endpoint . $authUrl);
    $data = json_decode($response->body());

    $slots = Redis::hget("rooms:$gamePath:$roomId", "slots");

    if ($data->occupied && $data->user_count >= $slots) return false;

    return [
        'provider_id' => $user->provider_id,
        'username' => $user->username,
        'avatar' => $user->avatar,
        'is_verified_creator' => $user->is_verified_creator
    ];
});

There's probably better ways of doing it but this seems to work soo shrug