Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 36 ยท Data Channels

Data Channel Reliability and Ordering

Choose between reliable and unreliable delivery for RTCDataChannel. Learn ordered, maxRetransmits and maxPacketLifeTime, head-of-line blocking, and which setup fits games or files.

TCP gives you every byte, in order, no matter how long it takes. UDP gives you whatever arrives, whenever. A WebRTC data channel lets you pick a point between those two extremes, per channel. That choice is one of the most important design decisions in a real-time app, because a late message can be worse than a lost one.

Imagine a multiplayer game sending a player position 60 times a second. If packet 41 gets lost, resending it is pointless: packet 42 already contains a newer position. A chat message, on the other hand, must arrive. In this lesson you will learn the three options that express this, and when to use each.

The three options

You pass them as the second argument to createDataChannel:

Option Default Effect
ordered true Messages are delivered in the order they were sent
maxRetransmits not set Give up after this many resend attempts
maxPacketLifeTime not set Give up after this many milliseconds

If you set neither maxRetransmits nor maxPacketLifeTime, the channel is fully reliable: SCTP retransmits until the message arrives (or the connection dies). Setting either one makes the channel partially reliable.

You may set only one of the two. Setting both throws a TypeError.

JavaScript
// default: reliable and ordered (like TCP)
pc.createDataChannel('chat');

// unordered but reliable: arrives eventually, any order
pc.createDataChannel('bulk', { ordered: false });

// unreliable and unordered (like raw UDP)
pc.createDataChannel('positions', { ordered: false, maxRetransmits: 0 });

// give up after 150 ms
pc.createDataChannel('voice-hint', { maxPacketLifeTime: 150 });

Head-of-line blocking

When a channel is ordered: true and one message is lost, every later message that already arrived must wait until the missing one is retransmitted and delivered. This delay is called head-of-line blocking.

sequenceDiagram
  participant S as Sender
  participant R as Receiver app
  S->>R: message 1
  S--xR: message 2 (lost)
  S->>R: message 3 (arrives, but held back)
  S->>R: message 4 (arrives, but held back)
  Note over R: waiting for message 2 to be retransmitted
  S->>R: message 2 (retransmitted)
  R-->>R: delivers 2, 3, 4 together

Ordered delivery turns one lost packet into a stall for everything behind it.

With ordered: false, messages 3 and 4 are handed to your code as soon as they arrive. With maxRetransmits: 0 message 2 is simply never delivered, so nothing waits at all.

Which setting for which job?

Reliable and ordered

Chat messages Every one must arrive
File transfer Bytes in exact order
Commands Join, leave, settings

The default. Safe, but can stall on loss.

1 / 3

A good rule: use reliable for data where every message changes the meaning, and unreliable for streams of state where the next message replaces the previous one. Many apps open one channel of each kind on the same connection.

Try it: reading the options back

On a local loopback there is no packet loss, so you cannot see retransmits, but you can confirm what was configured, and see the validation rules.

JS Reliability options

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice that maxRetransmits shows null when it is not set, and that asking for both limits is rejected.

Try it: unordered delivery in action

Even without loss, you should not depend on order when ordered is false. The standard pattern is to number your messages so the receiver can detect gaps and old data. Here the sender stamps each message with a sequence number, and the receiver ignores anything older than what it has already shown.

JS Newest state wins

Press Run (or Ctrl + Enter) to see the console output here.

No output.

On a real network the numbers differ: some messages never arrive (accepted is lower) and some arrive late (dropped as stale). Your code is right in both cases because it never trusts delivery order.

Practical advice

  • Start reliable and ordered. Switch to unreliable only when you measure a latency problem.
  • Use separate channels for separate needs. One reliable channel for commands, one unreliable channel for positions.
  • Prefer maxRetransmits: 0 for pure state streams, and maxPacketLifeTime when "fresh enough" is measured in time.
  • Do not set both limits; the browser throws.
  • Message size still matters, see Binary Data and Backpressure.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A channel is created without maxRetransmits and maxPacketLifeTime. What is its delivery mode?

    Without a limit SCTP retransmits until the message arrives, so the channel is fully reliable.
  2. 2. What does ordered: true cost you when a packet is lost?

    In-order delivery means messages that already arrived are held until the missing one is retransmitted.
  3. 3. Which settings suit a stream of player positions sent 60 times per second?

    Old positions are useless once a newer one arrives, so unordered and unreliable gives the lowest latency.
  4. 4. What happens if you set both maxRetransmits and maxPacketLifeTime?

    The specification forbids setting both; createDataChannel throws a TypeError.

Summary

  • Three options define delivery: ordered, maxRetransmits, maxPacketLifeTime.
  • No limit means reliable; one limit means partially reliable; both limits is an error.
  • Ordered delivery causes head-of-line blocking when packets are lost.
  • Use reliable channels for commands and files, unreliable ones for fast-changing state.
  • Number your messages when order is not guaranteed.

Next: Binary Data and Backpressure