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.
datachannel.go
Example Configurations
Sending Data
Text Messages
datachannel.go
Binary Data
datachannel.go
Receiving Data
OnMessage Handler
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
peerconnection.go
Buffered Amount
Monitor how much data is queued for sending:datachannel.go
datachannel.go
Pre-negotiated Data Channels
Create matching channels on both sides without in-band negotiation:Detached Mode
For advanced use cases, detach the data channel to get raw read/write access:datachannel.go
Closing Data Channels
datachannel.go
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