# Choosing how to send

> broadcast, send or sendReliable, and how to design for lost messages.

| | `broadcast` | `send` | `sendReliable` |
|---|---|---|---|
| to | everyone in the room | one peer | one peer |
| delivery | best effort: in lab tests 99.6–100% arrived | best effort | retransmitted until acknowledged |
| order | none promised | none promised | in order, per peer |
| size | ≤ 1,052 bytes | ≤ 1,052 bytes | ≤ 64 KiB |
| sealed | no (signed) | yes | yes |
| use for | frequent state everyone needs: presence, positions, live updates | frequent state for one peer | messages that must arrive: commands, chat, answers, a state snapshot for a late joiner |

> **Diagram: broadcast reaches every peer by hops and can be read by all, but is signed by its sender; send goes to one peer, sealed.** Left, broadcast(data): p1 sends to p2 and p3 (hop 1); p2 forwards to p4 and p5, p3 to p7 (hop 2); p4 forwards to p6 (hop 3). Every member can read a broadcast. Right, send(p6, data): the message goes p1 to p3 to p7 to p6, sealed with a lock; it is on its last hop. p3 and p7 forward it but cannot read or change it.

Every `broadcast` is signed by its sender and checked by every peer, so `from` is the real sender. It is not sealed: everyone in the room can read it. See [security and privacy](https://jsflow.com/docs/guides/security-and-privacy.md).

## Design for lost broadcasts

Most apps do best with small frequent deltas plus a way to repair gaps: number your messages, and when a receiver sees a gap, ask the sender with `sendReliable` or fetch it from your server. Sending the full state every few seconds also works.

## Channels

Channels (0 to 13) are yours: a label that arrives in `info.channel`, so you can route traffic without parsing it first. The default is 0. A channel outside 0 to 13 throws a `RangeError`.

```ts
import { join } from 'jsflow-sdk';

const CHAT = 0, PRESENCE = 1;
const room = await join({ app: 'pk_…', room: 'team-7' });

room.on('message', (from, data, info) => {
  if (info.channel === PRESENCE) applyPresence(from, data);
  else if (info.channel === CHAT) addChatLine(from, data);
});
room.broadcast(new Uint8Array([1, 2, 3]), { channel: PRESENCE });

declare function applyPresence(from: number, data: Uint8Array): void;
declare function addChatLine(from: number, data: Uint8Array): void;
```

## Bigger data

Anything over 1,052 bytes does not fit a `send` or `broadcast` (they throw a `RangeError`): use `sendReliable`, which takes up to 64 KiB (`MAX_RELIABLE`), and split anything larger into numbered `sendReliable` chunks. To get something to everyone reliably, send it to each peer, or let your server hold it.

## Rates

The lab tests ran one sender at 10 messages a second of 150 bytes to 29 receivers, plus reliable messages, with room to spare. Batch what you can: ten 150-byte batches a second costs far less than a hundred tiny messages.

## Before anyone is there

A broadcast sent before any peer is reachable reaches no one. Wait for `'connected'`, or send the late joiner what it needs when its `peer` event says it is up (from every peer for set-like state, from one for an ordered history: [keeping state in sync](https://jsflow.com/docs/guides/keeping-state-in-sync.md#2-give-the-state-to-whoever-arrives)). Use `sendReliable` for that: a broadcast sent the moment a peer comes up is best effort like any other, so send it again later if it matters.

## Errors when sending

Send errors are thrown synchronously, also by `sendReliable` (before it returns its promise): a message over its size limit or a channel outside 0 to 13 throws a `RangeError`, and any send once the room is `left`, `superseded` or `evicted` throws an `Error`. A `sendReliable` promise rejects only if the room stops before the message is queued.

Source: https://jsflow.com/docs/concepts/choosing-how-to-send/
