# Events

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

What an event is, its name, type, key and payload, and how a topic decides each event's type.

An **event** is one thing published to a topic: a chat message, a read receipt, a keystroke, a location fix.

| Part | What it is |
| --- | --- |
| Name | What happened: `message`, `read`, `typing`. 1–64 bytes of UTF-8 |
| Type | How Folkways keeps it: **stream**, **kv** or **signal** |
| Payload | Your data, as bytes. JSON is the usual choice; Folkways never reads it |
| Publisher | The sender's identity, set by Folkways from the verified token |
| Key | KV and signals only: which value it sets, e.g. a user or an item |
| Headers | Up to 16 string pairs your app defines, such as a schema version |
| Position | Stream only: a cursor `(epoch, seq)` and a permanent `uid` |

## Event types [#event-types]

| Type | Folkways keeps | A new subscriber gets | Use it for |
| --- | --- | --- | --- |
| [Stream](/docs/primitives/stream) | Every event, in order, within retention | A replay from a position it picks, then live events | Messages, orders, AI output |
| [KV](/docs/primitives/kv) | The latest value per event name and key | A snapshot of current values, then changes | Read receipts, scores, locations, status |
| [Signal](/docs/primitives/signal) | The latest value per event name and sender, for seconds | A snapshot of live signals, then new ones | Typing, cursors, "here now" |

<Diagram name="delivery-types" alt="Stream keeps every event in order and gives a new subscriber a replay from a chosen position, then every new event. KV keeps the latest value per key, so after read alice=#2, read bob=#3 and read alice=#4 a new subscriber gets a snapshot alice=#4, bob=#3, then each change. Signal keeps the latest per sender and expires it; a new subscriber gets the signals still alive, then new ones, best effort." />

## One topic, several types [#one-topic-several-types]

A topic rule lists the types its topics may carry, and can fix each event name to one type with an event map:

```json title="Topic rule"
{
  "pattern": "rooms/*",
  "types": ["stream", "kv", "signal"],
  "events": {
    "message": "stream",
    "read": { "delivery": "kv", "key_is_publisher": true },
    "typing": "signal"
  }
}
```

The type of an event is decided in this order:

1. If the rule's event map names the event, that type. Asking for another is refused with `ClassMismatch`.
2. If the rule has an event map and doesn't name the event, the publish is refused with `ClassMismatch`, unless
   the rule turns strict event names off.
3. Otherwise the type the publisher asked for.
4. Otherwise the rule's only type, or stream when it allows several.

Fixing names to types in the rule is the safest choice: a client can't send a keystroke as a stream event, or a
message as a signal.

## Rights per type [#rights-per-type]

Writing needs the right op in both the token and the rule:

| Type | Ops that allow writing it |
| --- | --- |
| Stream | `publish` |
| KV | `set` or `publish` |
| Signal | `signal` or `publish` |

## Keys [#keys]

KV values and signals are stored under a **key**. For KV it defaults to the publisher's identity, so `read` with no
key means "this user's read receipt". Set your own key for shared values, such as a score per match.

With `key_is_publisher` on a KV event, nobody but your backend can write or delete someone else's key, so a user
can't move another user's read receipt.

## Payloads [#payloads]

The payload is opaque bytes, at most 60 KiB (61,440 bytes), or less if the segment or rule sets a lower limit. A
**content type** label (JSON, text, bytes, FlatBuffers, Protobuf, MessagePack, CBOR, or your own) tells receivers
how to decode it; Folkways doesn't check it. See [Payload format](/docs/realtime-api/payloads).

## IDs: uid, cursor and display id [#ids-uid-cursor-and-display-id]

- Stream and KV events get a **uid**, a UUIDv7 that never changes. Point read receipts, edits and reactions at the
  uid.
- Stream events also have a **cursor** `(epoch, seq)`: their position, for resuming. A cursor can change meaning when
  the epoch changes, so never use it as a permanent reference.
- Signals get neither: they are meant to vanish.

See [Delivery and ordering](/docs/concepts/delivery) for cursors.
