> ## Documentation Index
> Fetch the complete documentation index at: https://docs.result.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Realtime

> Channels, events and presence over WebSocket.

Declare the channel pattern once (CLI), then connect from the app:

```bash theme={"dark"}
npx @resultdev/cli realtime add-channel "chat:%"   # % is a wildcard - covers every room
```

```ts theme={"dark"}
await backend.realtime.connect();
const res = await backend.realtime.subscribe("chat:room-1");
backend.realtime.on("new_message", (msg) => console.log(msg));
await backend.realtime.publish("chat:room-1", "new_message", { text: "Hi" });
```

<Warning>
  Subscribing to a channel no declared pattern covers fails with
  `REALTIME_UNAUTHORIZED`. Declare the pattern first - that alone unlocks it.
</Warning>

## Presence

* `res.presence.members` on subscribe - who is here now
* `presence:join` / `presence:leave` events as people come and go
* `backend.realtime.getPresenceState(channel)` any time

## Rules

1. `connect()` before anything else.
2. `publish()` requires a prior `subscribe()` to the same channel.
3. Leave with `unsubscribe(channel)`.
