Configure WebRTC RTCDataChannel Reliability
This article explains how to explicitly configure the WebRTC
RTCDataChannel for either reliable or unreliable packet
delivery. You will learn about the configuration parameters used during
channel creation, including how to mimic TCP-like reliability or
UDP-like speed and low latency depending on your application’s specific
requirements.
Yes, the WebRTC RTCDataChannel can be explicitly
configured for either reliable or unreliable packet delivery. By
default, a data channel acts like a traditional TCP
connection—guaranteeing that data packets arrive in order and without
loss. However, you can customize this behavior during the channel’s
creation to match UDP-like behavior for real-time applications like
gaming, VoIP, or live streaming.
How to Configure Reliability
Configuration is handled via the options object (of type
RTCDataChannelInit) passed as the second argument to the
RTCPeerConnection.createDataChannel() method.
Here is an example of how to create both types of channels:
const peerConnection = new RTCPeerConnection();
// 1. Reliable and Ordered (Default TCP-like behavior)
const reliableChannel = peerConnection.createDataChannel("reliableData");
// 2. Unreliable and Unordered (UDP-like behavior)
const unreliableChannel = peerConnection.createDataChannel("unreliableData", {
ordered: false,
maxRetransmits: 0 // No retransmissions allowed
});Key Configuration Parameters
To customize the reliability and ordering of packet delivery, you use three primary properties in the configuration object:
ordered(Boolean): Specifies whether the receiver must receive the packets in the exact order they were sent. Setting this tofalseallows packets to be processed as soon as they arrive, regardless of order. The default istrue.maxPacketLifeTime(Number): Specifies the maximum length of time, in milliseconds, that the browser will attempt to retransmit a failed packet. If this time is exceeded, the packet is discarded.maxRetransmits(Number): Specifies the maximum number of times the browser will attempt to retransmit a failed packet before giving up.
Configuration Rules and Limitations
When setting up custom reliability, keep the following rules in mind:
- Mutual Exclusion: You cannot specify both
maxPacketLifeTimeandmaxRetransmitsin the same configuration object. Attempting to set both will result in a syntax error or a failed channel creation. - Partially Reliable: If you set
orderedtotruebut limitmaxRetransmitsormaxPacketLifeTime, the channel becomes partially reliable. Packets will be delivered in order, but some packets may be lost if they exceed the defined retransmission limits. - Fully Unreliable: To achieve a true UDP-like
experience, set
orderedtofalseand set eithermaxRetransmits: 0or a very lowmaxPacketLifeTime. This ensures data is sent with minimum overhead, never retransmitted, and processed immediately upon arrival.