# Topics

URL: https://docs.folkways.io/docs/concepts/topics

Topic names and patterns, topic rules, public and private topics, personal topics, direct messages and system topics.

A **topic** is a named channel inside a segment, such as `chat/lobby`, `rooms/42` or `rides/7/location`. Apps
publish events to a topic and subscribe to it. You don't create topics ahead of time: a topic exists as soon as it
is first used, as long as a **topic rule** in the segment matches its name.

A topic is usually one *thing* in your app, like a chat room, a document or a match, and carries every kind of event
about it: messages, read receipts and typing indicators on one topic, with one permission and one subscription.

## Names and patterns [#names-and-patterns]

Topic names are ASCII:

- levels separated by `/`, each built from `A-Z a-z 0-9 . _ ~ : @ = + -`;
- at most 16 levels and 255 bytes;
- names starting with `$` are reserved for [system topics](#system-topics).

Rules and token capabilities match names with **patterns**:

| Pattern | Matches | Doesn't match |
| --- | --- | --- |
| `chat/lobby` | `chat/lobby` | `chat/lobby/2` |
| `rooms/*` | `rooms/42` | `rooms`, `rooms/42/typing` |
| `rides/*/location` | `rides/7/location` | `rides/7/eta` |
| `chat/>` | `chat/lobby`, `chat/team/7` | `chat` |

`*` matches exactly one level; a trailing `>` matches one or more. A subscription is always to one concrete topic;
to follow many, subscribe to each.

## Topic rules [#topic-rules]

A segment's topic rules decide which topics exist and what they carry. They are checked **in order and the first
match wins**; a topic no rule matches is refused with `Forbidden`.

A rule sets:

| Setting | Meaning |
| --- | --- |
| Pattern | Which topic names it covers |
| Event types | Which of Stream, KV and Signal its topics carry |
| Event names | Optional: fix each event name to one type, e.g. `message` → Stream, `read` → KV |
| Access | **Anyone the token allows** (public) or **Members only** (private) |
| Presence | Whether subscribers show up in [presence](/docs/primitives/presence) |
| Operations | The most any token may do on its topics |
| Limits | Retention for streams, lifetime and key count for KV and signals, a lower payload limit |

Rules are managed in the Console; see [Topic rules](/docs/console/topic-rules) for every field. The
[JavaScript SDK](/docs/js-sdk/topics#define-topics-and-export-rules) can generate a rule from typed code.

## Who may use a topic [#who-may-use-a-topic]

An operation is allowed only if the topic's rule, the token and, on a private topic, the member list all allow it.

<Diagram name="topic-access" alt="A request such as publish to rooms/42 is checked in order: does a topic rule match the topic, does the token grant the op on the topic, does the rule allow the op and event type. Any no is Forbidden. If the rule's access is members, the topic is private and the user's member entry must also allow the op; otherwise the topic is public and the request is allowed." />

`Forbidden` is the same answer for "no rule" and "no right", so a client can't probe which topics exist.

### Public topics [#public-topics]

With **Anyone the token allows** (`access: token`), the token decides. Put the topics a user may use in their
token, often as a pattern: `chat/team-7/>`.

### Private topics [#private-topics]

With **Members only** (`access: members`), each topic has a **member list** that your backend keeps, and a user may
do only what their token, the rule and their member entry all allow. Tokens then act as a ceiling, such as
`dm/>` with `subscribe` and `publish`, and don't need to list rooms.

- Adding a member grants them the operations you give, immediately.
- Removing a member ends their subscriptions there at once, and a fresh token doesn't bring them back.
- Narrowing a member to `subscribe` lets them read but not write.
- A topic holds up to 10,000 members.

Manage member lists with [`POST /v1/members/*`](/docs/realtime-api/members) or the SDK's
[`members` helpers](/docs/js-sdk/private-topics). Service tokens need no member entry.

## Personal topics [#personal-topics]

Every signed-in identity has a personal topic, `$user/{identity}`, for invitations and notices meant for one user.

- The user can subscribe to their own with no capability for it. Nobody else can read it.
- Only **service** tokens write to it, with `publish` on it, from your backend.
- It is a stream that keeps 256 KiB or 24 hours per user, whichever is less. Treat it as a **doorbell, not a
  mailbox**: keep the list of a user's rooms in your own backend, and use the personal topic to say "something
  changed".

The identity must be one topic level (no `/`) to have one.

## Direct messages and group rooms [#direct-messages-and-group-rooms]

Your backend opens a room for a set of 2 to 50 users with [`POST /v1/dm/open`](/docs/realtime-api/dm-open). Folkways
names the room `dm/{id}` (or another prefix you choose) from the sorted members, so the same members always get the
same room, and posts an `invite` on each member's personal topic. Their apps then subscribe to the room.

The room needs a topic rule like any other topic, usually members-only, in which case the members are added to its
member list for you.

## System topics [#system-topics]

Names starting with `$` belong to Folkways. Your topic rules never apply to them.

| Topic | Holds | Who reads it |
| --- | --- | --- |
| `$user/{identity}` | Invitations and notices for one user | That user |
| `$online/{identity}` | One entry per open connection of the identity; empty means offline | That identity, and tokens with `presence` on it |
| `$presence/{topic}` | One entry per connection present in the topic or below it | Tokens with `presence` on the topic |

See [Presence](/docs/primitives/presence) for `$online` and `$presence`.

## Lifecycle [#lifecycle]

A topic is created the first time it is used and counts toward your package's topics while it has subscribers,
retained events or current values. A topic nobody reads and with nothing retained is released after about a
minute. A released topic that is used again may start a new **epoch**, and then old cursors get a
[gap](/docs/concepts/delivery#gaps).
