# Presence

URL: https://docs.folkways.io/docs/primitives/presence

Who is online and who is in each topic, kept by Folkways from live connections, with optional status data.

Presence tells your app who is online and who is in a topic. Nobody publishes join or leave events: Folkways keeps
presence from the connections themselves, so it is right even when a phone loses signal.

There are two views, both [system topics](/docs/concepts/topics#system-topics) you subscribe to like any other:

| Topic | Holds | Typical use |
| --- | --- | --- |
| `$online/{identity}` | One entry per open connection of that identity. Empty means offline | A friends list, a "last seen" dot |
| `$presence/{topic}` | One entry per connection present in the topic or anywhere below it | "Who's in this room", viewer counts |

Both are KV topics: subscribe once and keep a map by key. The snapshot fills it, then each change updates it, and a
leave arrives as a deletion.

## Being present in a topic [#being-present-in-a-topic]

Turn on **presence** in a topic rule. Then subscribing to a matching topic makes the connection present in that
topic **and in every area above it**:

<Diagram name="presence" alt="When alice subscribes to chat/42/typing and its rule, chat/>, has presence on, alice's connection appears in $presence/chat/42/typing, $presence/chat/42 and $presence/chat, each entry keyed alice#session with device, since and data. While any of alice's connections is open she also has an entry in $online/alice, which is empty when she is offline." />

Unsubscribing, losing the right on a token refresh, or disconnecting removes the entry at once. A connection that
drops without closing cleanly is removed when Folkways notices, at worst after the 90-second idle timeout.

## Entries [#entries]

Each entry's key is `identity#session` (in `$online`, just the session), and its value is JSON:

```json
{ "identity": "user:7", "session": "9f1c…e2", "device": "ios", "since": 1792000000000, "data": { "status": "busy" } }
```

`device` is the label the client sent when connecting, if any. `data` is your own status data: publish up to 1,024
bytes of JSON to **your own** `$online/{identity}` and it appears in your online entry and in every presence entry of
that connection. Nobody can write anyone else's.

## Who may read [#who-may-read]

Reading presence needs the `presence` op, which is separate from `subscribe`:

- `{ "topic": "$online/>", "ops": ["presence"] }` lets a user see who is online;
- `{ "topic": "chat/>", "ops": ["presence"] }` lets them see who is in each chat topic.

A user can always read their own `$online`. A reader without the right gets `Forbidden`, whether or not the person
exists, so presence can't be used to look anyone up.

Presence of **private** topics is closed to client tokens: it would show who is in a private room. Your backend can
still read it.

## Staying out of presence [#staying-out-of-presence]

A connection can be **invisible**: it subscribes and publishes as usual but never appears in `$online` or
`$presence`. Set `"presence": false` in the token, or ask for it when connecting. Anonymous connections and HTTP
requests never appear in presence.

## Cost [#cost]

Presence entries and their deliveries don't count as billed messages.

## In code [#in-code]

See [Presence in the JavaScript SDK](/docs/js-sdk/presence), or [presence on the wire](/docs/realtime-api/subscribe#presence).
