# Presence channels

Authorize presence channels through Laravel, track distinct members across browser tabs, and subscribe with signed member tokens.

## Authorize and broadcast

Register the callback for the unprefixed name, as you would for a [private channel](./private-channels.md).

```php filename="routes/channels.php"
use App\Models\User;
use Illuminate\Support\Facades\Broadcast;

Broadcast::channel('room', function (User $user) {
    return ['name' => $user->name];
});
```

This callback lets any authenticated user join. Apply your application's access rules in the callback. Return a nonempty array to supply member info, another truthy value to supply identity alone, or a falsy value to deny access. Murmur uses `getAuthIdentifierForBroadcasting()` when present, otherwise `getAuthIdentifier()`. A string or integer identifier becomes a string member ID. Invalid member data returns 403.

Send `{"channel_name":"presence-room"}` to `POST /broadcasting/auth`. On success Laravel returns `{"token":"<JWT>","expires_at":1710000900}`. Return `new PresenceChannel('room')` from your broadcast event's `broadcastOn()`, using `Illuminate\Broadcasting\PresenceChannel`.

## Issue a member token

To authorize a subscription in your own Laravel endpoint, check access first and derive the member from the authenticated user. Use the same ID as Laravel’s broadcasting authorization endpoint. Renewing with a different ID removes the presence topic from the stream.

```php
use Arttecs\Murmur\Facades\Murmur;

$id = method_exists($user, 'getAuthIdentifierForBroadcasting')
    ? $user->getAuthIdentifierForBroadcasting()
    : $user->getAuthIdentifier();
abort_unless(is_string($id) || is_int($id), 403);

$member = ['id' => (string) $id, 'info' => ['name' => $user->name]];
$token = Murmur::token(['presence-room'], member: $member);
$url = Murmur::stream('presence-room')->url(member: $member);
```

One member claim applies to the presence topics covered by that token. For your own credential format, return a [grant with a member](./extending.md#implement-a-subscription-authorizer).

## How membership works

Each user ID appears once per topic, even across several tabs or streams. The first join fixes the member's info until the member has fully left. After the last stream leaves and the configured leave grace expires, the server removes the member. A reconnect during grace can keep the member in the list. There is no separate `me` field. Use your application's signed-in user ID to identify the current member.

The member limit applies to distinct IDs. When a new ID would exceed the limit, the server reports `full`. The subscription can still receive ordinary broadcasts. Leave and rejoin the topic to retry. If the server reports `unavailable`, it retries membership automatically.

Every `presence-*` topic requires a member credential. A server that omits the `presence` capability still delivers ordinary broadcasts on authorized topics. See [configuration](./configuration.md#presence-and-client-events), the [wire frames](./wire-protocol.md#presence-frames), and the [JavaScript API](./javascript.md#presence-channels).
