Skip to main content

Overview

Data channels provide a way to send arbitrary application data between peers using the SCTP protocol over DTLS. Unlike media tracks, data channels can send any binary or text data with configurable reliability and ordering guarantees.

DataChannel Structure

From the Pion source code:
datachannel.go

Creating Data Channels

Creating on Peer Connection

With Configuration

Data channels created with CreateDataChannel() are negotiated in-band. The remote peer receives them via the OnDataChannel callback.

Data Channel Options

Reliability Options

Reliable Ordered

All messages delivered in order (default).

Reliable Unordered

All messages delivered, order not guaranteed.

Unreliable with Retransmits

Limited retransmissions, may lose messages.

Unreliable with Lifetime

Messages dropped after time limit.
From the source code, here’s how channel types are determined:
datachannel.go

Example Configurations

Sending Data

Text Messages

datachannel.go
Usage:

Binary Data

datachannel.go
Usage:
Always check if the data channel is open before sending. Sending on a closed channel will return an error.

Receiving Data

OnMessage Handler

The message structure:
datachannelmessage.go

Reading Loop Implementation

From the source code:
datachannel.go

Data Channel States

Data channels go through several states:
1

Connecting

Channel is being established.
2

Open

Channel is open and ready to send/receive.
3

Closing

Close has been called, but not complete.
4

Closed

Channel is closed.

Monitoring State

Receiving Data Channels

From the source:
peerconnection.go

Buffered Amount

Monitor how much data is queued for sending:
datachannel.go
Usage:
From the source:
datachannel.go

Pre-negotiated Data Channels

Create matching channels on both sides without in-band negotiation:
Pre-negotiated channels don’t require signaling and can be created before the connection is established.

Detached Mode

For advanced use cases, detach the data channel to get raw read/write access:
datachannel.go
Usage:

Closing Data Channels

datachannel.go
Usage:

Common Patterns

Request/Response

File Transfer

Multiplexing Channels

Performance Tips

Message Size

Keep messages under 16KB for best performance. Larger messages are fragmented.

Buffering

Monitor BufferedAmount() to avoid overwhelming the send buffer.

Reliability

Use unreliable channels for real-time data where freshness matters more than reliability.

Multiple Channels

Create separate channels for different data types to avoid head-of-line blocking.

Next Steps

WebRTC Overview

Review core WebRTC concepts

ICE & Connectivity

Learn about network connectivity