channel option
of the network methods, for example
ctx.network.sendData(data, { channel: Channel.UnreliableOrdered }). Without
it, packets are sent on Channel.ReliableOrdered, and typed packets on the
channel of their definition. Pick the channel by what the
packet needs: whether it may be lost, and whether an older packet is still
useful once a newer one has arrived.
The reliable channels need a TCP port (
SERVER_TCP_PORT / LISTENING_TCP_PORT),
the unreliable ones a UDP port (SERVER_UDP_PORT / LISTENING_UDP_PORT).
Using a channel whose port is not configured throws, except on a
peer-to-peer host, where every channel works.
Over TCP, ReliableUnordered is delivered in order in practice: it only
promises that nothing is lost.
Framing
Both transports deliver whole messages, so one message is one packet: there is no delimiter and payloads may contain any bytes. Every packet starts with the name of its packet type: one length byte, then the name in UTF-8, then the payload.- Typed packets (
ctx.network.packets.send, see Typed packets) carry the name given todefinePacket, and their payload is theJSON.stringifytext of the data, or nothing for a packet without payload. The receiver routes them to the listeners andreadof that type. sendDatapackets have a length of0, so no name. Their payload carries no format tag: bytes are sent as they are, a string as its UTF-8 text, and any other value as itsJSON.stringifytext. The receiver gets aNetworkDatafromgetReceivedPacketsand reads it the way it was sent, withbytes(),text()orjson().
- TCP: each binary WebSocket frame starts with one tag byte,
0forReliableOrderedand1forReliableUnordered, followed by the packet. Text frames are reserved for control messages such as the serverwelcome. - WebRTC: the client opens one data channel per unreliable channel, labelled
unreliable-orderedandunreliable-unordered. Both useordered: falseandmaxRetransmits: 0.
Dropping late packets
EachUnreliableOrdered message starts with a big-endian uint32 sequence
number, followed by the packet; the number is counted per peer and per
direction. The receiver delivers a packet only if it is
newer than the last one it delivered, and drops it otherwise. Nothing ever waits
for a lost packet: a gap is skipped as soon as a newer packet arrives. Numbers
are compared as serial numbers, so the counter can wrap around.
The sequence is shared by every packet type sent on the channel: a late packet
is dropped even when the newer one was of another type.