# Primitives

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

Stream, KV, Signal and Presence, the four ways Folkways keeps and delivers live data, and how to choose between them.

Every event on a topic is one of three types, and Folkways adds a fourth primitive, presence, on top of them. They
are features of the service, not of an SDK: every client, in any language, gets the same behaviour.

<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." />

| | [Stream](/docs/primitives/stream) | [KV](/docs/primitives/kv) | [Signal](/docs/primitives/signal) | [Presence](/docs/primitives/presence) |
| --- | --- | --- | --- | --- |
| Keeps | Every event, in order | Latest value per key | Latest value per sender | One entry per connection |
| For how long | Up to 5 MiB, at most 24 h | Until replaced, deleted or expired | 5 s by default | While connected |
| New subscriber gets | Replay from a position, then live | Snapshot, then changes | Live signals, then new ones | Snapshot, then changes |
| Ordered | Yes, per topic | Latest wins | Latest wins | Latest wins |
| Retried safely | Yes, idempotency key | Latest wins | No: best effort | Written by Folkways |
| Op to write | `publish` | `set` | `signal` | none: automatic |
| Typical use | Messages, orders, AI tokens | Read receipts, scores, locations | Typing, cursors | Online lists, who's in a room |

## Choosing [#choosing]

- **Would a user be upset to miss it?** Use a **stream**: messages, orders, notifications.
- **Do you only care about the current value?** Use **KV**: where each driver is, who read up to where, the
  score.
- **Is it stale within seconds?** Use a **signal**: typing, cursor position, "is speaking".
- **Do you want to know who's around?** Turn on **presence** for the topic's rule; nobody publishes anything.

A chat room uses all four on one topic: `message` as a stream, `read` as KV, `typing` as a signal, and presence to
list who's in the room. See the [chat room example](/docs/examples/chat-room).

## Using them [#using-them]

- From JavaScript: [publishing](/docs/js-sdk/publishing), [KV](/docs/js-sdk/kv), [signals](/docs/js-sdk/signals),
  [presence](/docs/js-sdk/presence).
- From any language: the [`Publish`](/docs/realtime-api/publish) and [`Subscribe`](/docs/realtime-api/subscribe)
  frames of the Realtime API.
