Choosing how to send
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 |
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.
Design for lost broadcasts
Section titled “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
Section titled “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.
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
Section titled “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.
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
Section titled “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). 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
Section titled “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.