RTCDataChannel
Signature
const channel = pc.createDataChannel(label, options);
// options: { ordered, maxRetransmits, maxPacketLifeTime, negotiated, id }
What it does
Carries arbitrary data (text, JSON, or binary) directly between peers over SCTP, without a server in the path. Delivery can be reliable and ordered or fast and lossy.
Key members
| Member | Kind | Purpose |
|---|---|---|
label |
property | The channel name set at creation. |
readyState |
property | connecting, open, closing, closed. |
ordered |
property | Whether messages arrive in send order. |
bufferedAmount |
property | Bytes queued but not yet sent. |
bufferedAmountLowThreshold |
property | Threshold that fires bufferedamountlow. |
binaryType |
property | blob or arraybuffer for incoming binary. |
send() |
method | Send a string, ArrayBuffer, Blob, or typed array. |
close() |
method | Close the channel. |
open / message / close / error |
event | Lifecycle and inbound data. |
Behaviour & constraints
- Only call
send()whenreadyState === 'open', or it throws. maxRetransmitsandmaxPacketLifeTimeare mutually exclusive; either makes the channel unreliable and implies unordered-friendly use.- Set
negotiated: truewith a fixedidto open both ends without thedatachannelevent. - Watch
bufferedAmountto apply backpressure on large transfers; don't flood the buffer. - The non-creating peer receives the channel via the connection's
datachannelevent.
Example
const channel = pc.createDataChannel('chat');
channel.onopen = () => channel.send('hello');
channel.onmessage = (e) => console.log('peer said', e.data);
// remote side
pc.ondatachannel = ({ channel }) => {
channel.onmessage = (e) => console.log(e.data);
};