Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 36 ยท Data Channels

Data Channels: Sending Messages Between Peers

Learn how RTCDataChannel sends text and binary data directly between browsers: create a channel, handle open, message and close events, and test it in a loopback demo.

Audio and video get all the attention in WebRTC, but the same peer-to-peer connection can also carry arbitrary data. An RTCDataChannel is a two-way pipe between two browsers that behaves a lot like a WebSocket, with one big difference: the bytes travel directly from peer to peer, with no server in the middle once the connection is up.

Data channels power multiplayer games, file sharing, collaborative editors, remote-control tools, and chat that never touches your backend. In this lesson you will create a channel, receive it on the other side, react to its life-cycle events, and see what happens under the hood. A connection between two peers must already be possible; if you need a refresher on offers and answers, open the WebRTC track page.

How a data channel travels

A data channel does not use its own network connection. It rides on the same secure transport that carries your media. Messages are framed by SCTP (Stream Control Transmission Protocol), and SCTP runs inside DTLS, the encryption layer, which in turn runs over UDP and the ICE-selected path.

flowchart TD
  A["Your app: channel.send(...)"] --> B["SCTP: streams, framing, optional retransmits"]
  B --> C["DTLS: encryption and integrity"]
  C --> D["ICE-selected UDP path"]
  D --> E["Remote peer"]

One SCTP association carries every data channel on a connection; each channel is a separate stream inside it.

Two consequences matter for you:

  • Data channels are always encrypted. You cannot turn that off.
  • Many channels share one transport, so adding a channel is cheap. You do not need to reconnect.

Creating a channel

The peer that wants a channel calls createDataChannel(label, options). The label is just a name you choose; it helps the other side tell channels apart.

JavaScript
const channel = pc.createDataChannel('chat');

The very first data channel changes the offer: the SDP gets an m=application section that describes the SCTP association. That is why you should create the channel before calling createOffer(). If you add a channel after the connection is already negotiated, the first one triggers a new negotiation (negotiationneeded fires).

The other peer does not create anything. It waits for the datachannel event on its own RTCPeerConnection:

JavaScript
pc.ondatachannel = (event) => {
  const channel = event.channel;
  console.log('Received channel:', channel.label);
};

The life cycle: readyState and events

A channel has a readyState that moves forward through four values:

State Meaning
connecting Created, but the SCTP association or the channel is not ready yet
open Ready; send() works
closing close() was called and pending messages are being flushed
closed The channel is finished and cannot be reused

The matching events are open, closing, close, message and error. Calling send() while the channel is not open throws an InvalidStateError, so always send after onopen or check readyState first.

Try it: a chat in one page

You cannot open a connection to a stranger inside a sandboxed preview, but you can connect two peers in the same page. The helper below wires two RTCPeerConnection objects together by passing their descriptions and candidates directly. In a real app that exchange goes through a signalling server.

JS Two peers, one data channel

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

No output.

Notice how the same RTCDataChannel object appears on both sides: Alice holds the one she created, Bob gets his own object from the datachannel event, and they are two ends of the same pipe.

Channel properties worth knowing

Every channel exposes read-only properties that describe how it was set up. You will use the reliability ones in Reliability and Ordering.

  • label: the name given at creation.
  • ordered, maxRetransmits, maxPacketLifeTime: delivery rules.
  • protocol: a free string for your own sub-protocol name, such as json-v1.
  • negotiated and id: used when both sides create the channel themselves (see Protocols and Patterns).
  • bufferedAmount: bytes queued but not yet sent (see Binary Data and Backpressure).
  • binaryType: how binary messages arrive (blob or arraybuffer).
JS Inspect a channel and its states

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

No output.

Common mistakes

  • Sending before the channel is open. Wait for onopen.
  • Creating the first channel after the offer. It works, but forces a second negotiation. Create channels before the first createOffer().
  • Reading event.channel on the creator. Only the remote peer gets datachannel. The creator already has the object.
  • Assuming a channel survives a dropped connection. When the peer connection fails, channels close. Reconnect logic belongs to you.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which protocol stack carries data channel messages?

    Data channels use SCTP for framing, wrapped in DTLS for encryption, on the UDP path chosen by ICE.
  2. 2. Which peer receives the ondatachannel event?

    The creator already holds the channel object; the remote peer is told about it through the datachannel event.
  3. 3. What happens if you call send() while readyState is connecting?

    send() requires the open state. Wait for the open event first.
  4. 4. Why create your first data channel before createOffer()?

    The first channel adds an application m-line to the SDP. Adding it later triggers negotiationneeded.

Summary

  • A data channel is an encrypted peer-to-peer message pipe on the same connection as your media.
  • The creator calls createDataChannel(label); the other peer receives it in ondatachannel.
  • readyState goes connecting, open, closing, closed; send only when open.
  • Events: open, message, error, closing, close.
  • Create channels before the first offer to avoid an extra negotiation.

Next: Reliability and Ordering