# Payload format

URL: https://docs.folkways.io/docs/realtime-api/payloads

How events travel, from your payload bytes to frames, packets and WebSocket messages, and the content types receivers decode by.

There are two layers, and they are independent:

1. **Your payload**: the bytes of your event, in whatever format you choose. JSON is the usual choice. Folkways
   never parses it.
2. **The protocol**: FlatBuffers frames that carry your payload with its topic, name, type and headers. Clients use
   it whether the payload is JSON or binary. The JavaScript SDK handles it for you.

You don't need FlatBuffers to send JSON. And labelling a payload as FlatBuffers doesn't make Folkways read it.

<Diagram name="packet-structure" alt="One WebSocket binary message, or one HTTP request body, is one packet: a FlatBuffers buffer with identifier RTEC from clients or RTES from servers, at most 64 KiB. A packet holds 1 to 64 frames, each with a corr request ID and a body such as Publish. A Publish frame has a topic, name, delivery type, idempotency key, key and ttl, up to 16 headers, a content type and the payload: your bytes, at most 60 KiB, never parsed. HTTP responses carry several server packets, each prefixed by its little-endian u32 length." />

## Messages, packets and frames [#messages-packets-and-frames]

| Layer | What it is | Limit |
| --- | --- | --- |
| **Message** | One WebSocket binary message, or one HTTP request body | One packet |
| **Packet** | One FlatBuffers buffer. Client packets have the file identifier `RTEC`, server packets `RTES` | 64 KiB |
| **Frame** | `{ corr, body }`: one request or reply. `corr` is your request ID, echoed on replies | 1 to 64 per packet |
| **Event** | Inside `Deliver` and `Snapshot` frames: one published item, with its payload | Payload 60 KiB |

HTTP responses carry a sequence of server packets, each prefixed by its length as a little-endian `u32`:
`[u32 length][packet][u32 length][packet]…`.

The [frame reference](/docs/realtime-api/frames) lists every frame and field.

## Content types [#content-types]

Each event carries a content type: a 16-bit label telling receivers how to decode the payload. It is a hint, not
validation.

| Name | Value | Use |
| --- | ---: | --- |
| `Unspecified` | 0 | No declared format |
| `Bytes` | 1 | Opaque bytes, such as encrypted envelopes |
| `Json` | 2 | Objects and values; the default for most apps |
| `Text` | 3 | UTF-8 text |
| `FlatBuffers` | 4 | Generated binary structures; see [FlatBuffers](/docs/realtime-api/payloads/flatbuffers) |
| `Protobuf` | 5 | Schema-defined binary messages |
| `MsgPack` | 6 | Compact objects |
| `Cbor` | 7 | Compact objects, common in IoT |
| your own | 1024–65535 | Your own encoding; agree on meanings across your apps |

Values 8 to 1023 are reserved for future standard formats.

**Which to choose.** JSON is simplest for web apps. FlatBuffers lets readers access fields without unpacking the
whole object. Protobuf has mature schemas and bindings everywhere. MessagePack and CBOR are compact object encodings.
Measure with your own data before switching for size or speed. Don't base64-encode binary payloads into JSON: it adds
about a third to their size.

## Headers [#headers]

Up to 16 string pairs per event, passed to every subscriber unchanged. Use them for metadata such as a schema
version or a trace ID, never for secrets: anyone who can read the topic can read them.

## The schema [#the-schema]

The protocol's FlatBuffers schema is
[`protocol.fbs`](https://github.com/Folkways/folkways-js/blob/main/packages/realtime/proto/protocol.fbs) in the
JavaScript SDK's repository. Generate bindings for your language with `flatc` 24.3.25 (`--ts`, `--python`, `--go`,
`--java`, `--kotlin`, `--swift` and others).

The schema evolves without breaking old clients:

| Rule | Effect |
| --- | --- |
| Fields are only added at the end of a table | Old clients ignore fields they don't know |
| Tags and enum values are never reused | A retired number stays empty |
| Unknown frame types are refused, never guessed | A safe failure instead of a misread |
| Zero is always the cautious default | An unset field behaves the way an old client expects |
| A breaking change would be a new version with new identifiers | It would run beside v1 |

## In this section [#in-this-section]

- [FlatBuffers](/docs/realtime-api/payloads/flatbuffers): an application schema, encoding, decoding and
  cross-language checks.
- [Protobuf, MessagePack and CBOR](/docs/realtime-api/payloads/other-formats).
- [Schemas: validation and evolution](/docs/realtime-api/payloads/schemas).
- [Size and compression](/docs/realtime-api/payloads/size-and-compression).
