Skip to content

Datagrams

Datagrams carry small, frequent, disposable values: cursors, typing indicators, positions, telemetry. They are declared in the contract as datagram topics (§12.2) with a payload type, a direction and a mode:

  • latest: only the newest value per key matters; older ones may be dropped.
  • all: every value matters, but loss is tolerated (best effort).

To keep datagrams small, each side maps topic names to small integer keys in its own sending key space.

  • Before using a key, the sender sends DGRAM_BIND {29: key, 30: name} on stream 0. name is a topic name, optionally followed by : and an instance identifier, e.g. "cursor:doc-42"; the topic is the part before the first :. Names use the characters of §10.1 and are at most 255 bytes.
  • A DGRAM_BIND whose name does not match this syntax, whose topic is not declared in the contract, or whose direction (§12.2) does not allow that sender, is ignored and its key stays unbound, so its datagrams are dropped like those of any unbound key.
  • Keys MUST NOT be rebound within a session. A sender MUST NOT bind more than 256 keys per direction, a fixed protocol value. A DGRAM_BIND for an already bound key, or beyond 256 keys, is a PROTOCOL_ERROR.
  • DGRAM_BIND is reliable and precedes use; a datagram with an unbound key MUST be dropped silently (it may have raced ahead of its binding on WebTransport, so this is not an error).

A WebTransport datagram payload is:

Datagram {
Type (8 bits) = 0x40,
Key (varint),
Seq (varint),
Data (remaining bytes, session codec)
}
  • Seq starts at 1 per key per direction and increases by 1 per datagram sent for that key. On resumption both peers reset datagram seq: senders restart every key at 1 and receivers clear their highest-delivered values (datagrams are never replayed, so their sequence space does not outlive a transport).
  • Receivers for latest topics MUST drop a datagram whose seq is ≤ the highest seq already delivered for that key.
  • The total size MUST NOT exceed the smaller of limits.max_dgram and the transport’s current maximum datagram size (browsers expose it as maxDatagramSize). Oversized values are rejected locally; they are never fragmented.
  • A receiver drops, without error, a datagram that fails to decode (wrong type byte, truncated varint) or exceeds its own max_dgram; it still counts toward the datagram rate limit (§14).
  • Datagrams are sent as DATAGRAM frames on stream 0 with header {29: key, 12: seq} and the value as data section.
  • They are reliable and ordered (TCP), so senders MUST emulate the latency behavior of unreliable delivery:
    • For latest topics, a sender keeps at most one pending unsent datagram per key, replacing it when a newer value arrives (coalescing).
    • Senders send DATAGRAM frames in scheduling class 5 (§6.6), and MAY drop all-mode datagrams when their outbound buffer exceeds a threshold.
  • The same max_dgram limit applies, so applications behave identically on both bindings. It applies to the size the datagram would have as a WebTransport payload (§11.3: type, key, seq and data). A DATAGRAM frame whose value exceeds the receiver’s max_dgram is a PROTOCOL_ERROR, because it came through the reliable framed path.
  • seq follows §11.3 and is assigned when the frame is written; a value replaced by coalescing consumes no seq.
  • Client → server datagrams are delivered to the server’s handler for that topic. The server decides whether to fan them out (e.g. relaying a cursor to other subscribers of a document channel).
  • Server → client datagrams are produced by server-side targeting (§13). A datagram whose payload does not encode is refused to the code that sends it, as §12.3 says.
  • Datagrams are not sequenced frames: they are never replayed on resumption and never stored in history.
  • An undecodable datagram value is handled per §8.10.

From the specification: §11.1, §11.2, §11.3, §11.4, §11.5.