# Presence
Source: https://sovaria.in/docs/realtime-presence

See who is connected to a channel: how presence is tracked, how long someone stays listed after disconnecting, several tabs and devices, and privacy.

Presence answers "who is here right now?" for a channel: online dots, "3 people are viewing", the avatars of collaborators in a document. Everyone connected to a channel is listed; nothing extra has to be sent.

> **Tip:** Need each person's state too (name, cursor, "typing…")? supabase-style presence (`channel.track(state)` / `presenceState()` with join and leave events) is on [Broadcast, presence and table changes](https://sovaria.in/docs/realtime-broadcast-and-presence).

## Read who is here

JavaScript:
```js
// Open the channel: this connection now counts as present
const ch = sovaria.realtime.channel('doc-1234', { onMessage: render })

// Who else is here
const here = await sovaria.realtime.presence('doc-1234')
// { channel: 'doc-1234', online_clients: ['u_7f3…', 'u_91a…'], count: 2 }
```

Python:
```python
from sovaria.realtime import RealtimeClient

room = RealtimeClient(sov).channel("doc-1234")
here = await room.presence()
print(here["count"], here["online_clients"])
```

cURL:
```bash
curl -s "$URL/api/v1/rt/stream/presence/doc-1234" -H "Authorization: Bearer $TOKEN"
```

## Who is listed

| Connected with | Listed as |
|---|---|
| A WebSocket channel (`realtime.channel`) as a signed-in user | The user id, **once**, however many tabs or devices that user has open |
| The subscribe API with a client id | `<user id>:<client id>`, one entry per tab or device |
| No signed-in user | `anon:<client id>`, so an anonymous client can never appear as, or remove, a real user |

Because ids are tied to the verified caller, nobody can mark someone else present or remove them. To show names and avatars, look the user ids up in your own `profiles` table (with rules that let channel members read them).

## How long someone stays listed

- An open connection heartbeats every **30 seconds** and stays listed while it beats.
- A connection that closes cleanly leaves the list at once.
- One that vanishes (lost network, a killed app) is removed after **90 seconds** without a heartbeat.
- Presence is shared by every realtime server, so all clients see the same list wherever they are connected.

## Instant join and leave

Presence is read on demand. Poll it every 15 to 30 seconds for a count, or, for instant avatars, have each client send a small message when it joins and when it leaves, and keep the list from those messages. Polling then only corrects for clients that vanished.

JavaScript:
```js
const people = new Map()
const ch = sovaria.realtime.channel('doc-1234', {
  onMessage: (m) => {
    if (m.type === 'joined') people.set(m.user_id, m.name)
    if (m.type === 'left') people.delete(m.user_id)
    renderAvatars([...people.values()])
  },
  onStatus: (s) => { if (s === 'live') ch.send({ type: 'joined', user_id: me.id, name: me.name }) },
})
window.addEventListener('pagehide', () => { ch.send({ type: 'left', user_id: me.id }); ch.close() })
```

## Who can read presence

- Only callers allowed to **subscribe** to the channel can read its presence; the same channel rules apply ([Channels and connections](https://sovaria.in/docs/realtime-channels)).
- Presence lists ids, never what someone is doing. Don't put personal data such as email addresses in channel names.
- Listing every channel in a project is for the project team, not for your app's users.

|  | Free | Pro | Ultra | Team | Enterprise |
|---|---|---|---|---|---|
| Realtime presence | ✓ | ✓ | ✓ | ✓ | ✓ |
| Realtime connections per signed-in user | 20 | 50 | 100 | 200 | 500 |
