# Size and compression

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

Payload and packet size limits, measuring encoded size, compressing payloads, and what to do with large data.

| Limit | Default |
| --- | --- |
| Payload | 61,440 bytes (60 KiB), or lower if the segment or rule says so |
| Packet (WebSocket message or HTTP body) | 65,536 bytes (64 KiB), framing included |
| Headers per event | 16 |
| Frames per packet | 64 |

The limits in force arrive in `Welcome.limits` (`session.limits` in the JavaScript SDK). A payload over the limit is
refused with `PayloadTooLarge`; a packet over it closes the connection with `FrameTooLarge`.

## Measure bytes, not characters [#measure-bytes-not-characters]

Measure the encoded bytes, not a string's length: a UTF-8 character can take up to 4 bytes, and JSON adds quotes,
keys and escapes. Encryption adds a nonce and a tag; framing adds the event's name, topic and headers.

```ts
const bytes = new TextEncoder().encode(JSON.stringify(data)).byteLength;
```

## Compression [#compression]

Folkways doesn't compress or decompress payloads. You can compress before publishing, with an agreed content type
or header, and decompress on receipt:

- cap the decompressed size, and refuse anything over it;
- don't compress secrets together with data an attacker controls.

## Large data [#large-data]

For documents, images or long AI output, store the object in your own storage and publish a small reference: an ID,
a signed URL with an expiry, a checksum. To send something larger in pieces, design the chunking yourself, with
bounded reassembly and a way to recover a missing piece. Chunking doesn't get around quotas, and every chunk is a
message.
