Skip to main content

Overview

The DataChannel represents a network channel for bidirectional peer-to-peer transfers of arbitrary data. Data channels are used to send and receive text and binary data between peers in a WebRTC connection.

Type Definition

The DataChannel manages reliability, ordering, and message delivery based on its configuration parameters.

Creating a DataChannel

NewDataChannel

Creates a new DataChannel. This constructor is part of the ORTC API and is not meant to be used together with the basic WebRTC API.
*SCTPTransport
required
The SCTP transport instance to send data over.
*DataChannelParameters
required
Configuration parameters for the data channel including label, protocol, ordering, and reliability settings.
*DataChannel
The newly created DataChannel instance.
error
Returns an error if the label exceeds 65535 characters or if the channel cannot be opened.

Properties

Label

Returns the label that distinguishes this DataChannel from other DataChannel objects. Scripts can create multiple DataChannel objects with the same label.

ID

Returns the ID for this DataChannel. The value is initially null if the ID was not provided at channel creation time and the DTLS role of the SCTP transport has not yet been negotiated. After the ID is set to a non-null value, it will not change.

ReadyState

Returns the current state of the DataChannel object.
DataChannelState
One of: DataChannelStateConnecting, DataChannelStateOpen, DataChannelStateClosing, or DataChannelStateClosed.

Protocol

Returns the name of the sub-protocol used with this DataChannel.

Ordered

Returns true if the DataChannel is ordered, and false if out-of-order delivery is allowed.

MaxPacketLifeTime

Returns the length of the time window (in milliseconds) during which transmissions and retransmissions may occur in unreliable mode.

MaxRetransmits

Returns the maximum number of retransmissions attempted in unreliable mode.

Negotiated

Returns whether this DataChannel was negotiated by the application (true), or not (false).

Transport

Returns the SCTPTransport instance the DataChannel is sending over.

Sending Data

Send

Sends binary data to the DataChannel peer.
[]byte
required
The binary data to send.
error
Returns an error if the channel is not open or if the write fails.

SendText

Sends a text message to the DataChannel peer.
string
required
The text message to send.
error
Returns an error if the channel is not open or if the write fails.

Event Handlers

OnOpen

Sets an event handler invoked when the underlying data transport has been established (or re-established).
func()
required
The callback function to invoke when the channel opens.
If the data channel is already open when OnOpen is called, the handler will be invoked immediately.

OnDial

Sets an event handler invoked when the peer has been dialed, but before the peer has responded.
func()
required
The callback function to invoke when dialing begins.

OnMessage

Sets an event handler invoked on a message arrival over the SCTP transport from a remote peer.
func(msg DataChannelMessage)
required
The callback function to invoke when a message is received.
OnMessage can currently receive messages up to 16384 bytes in size. Check out the Detach API if you need larger message sizes. Note that browser support for larger messages is also limited.

OnClose

Sets an event handler invoked when the underlying data transport has been closed.
func()
required
The callback function to invoke when the channel closes.
Due to backwards compatibility, there is a chance that OnClose can be called even if GracefulClose is used. If this is problematic, deregister OnClose prior to calling GracefulClose.

OnError

Sets an event handler invoked when the underlying data transport cannot be read.
func(err error)
required
The callback function to invoke when an error occurs.

Buffered Amount

BufferedAmount

Returns the number of bytes of application data (UTF-8 text and binary data) that have been queued using Send() or SendText(). The value does not include framing overhead incurred by the protocol or buffering done by the operating system or network hardware. The buffered amount only increases with each call to the send methods as long as the ReadyState is open. The value does not reset to zero once the channel closes.

BufferedAmountLowThreshold

Returns the threshold at which the bufferedAmount is considered to be low. When the bufferedAmount decreases from above this threshold to equal or below it, the OnBufferedAmountLow event fires. The threshold is initially zero on each new DataChannel, but the application may change its value at any time.

SetBufferedAmountLowThreshold

Updates the buffered amount low threshold.
uint64
required
The new threshold value in bytes.

OnBufferedAmountLow

Sets an event handler invoked when the number of bytes of outgoing data becomes lower than or equal to the BufferedAmountLowThreshold.
func()
required
The callback function to invoke when the buffered amount becomes low.

Advanced Operations

Detach

Detaches the underlying datachannel, providing an idiomatic io.ReadWriteCloser API with .Read() and .Write() methods instead of .Send() and .OnMessage().
Before calling Detach, you must enable this behavior by calling webrtc.DetachDataChannels(). Combining detached and normal data channels is not supported. This also disables the OnMessage callback.
datachannel.ReadWriteCloser
A ReadWriteCloser interface for reading and writing data.
error
Returns an error if detach is not enabled or if called before the channel is opened.

DetachWithDeadline

Detaches the underlying datachannel with deadline support. This is the same as Detach() but returns a ReadWriteCloserDeadliner.
datachannel.ReadWriteCloserDeadliner
A ReadWriteCloserDeadliner interface with deadline support.
error
Returns an error if detach is not enabled or if called before the channel is opened.

Closing the Channel

Close

Closes the DataChannel. It may be called regardless of whether the DataChannel object was created by this peer or the remote peer.
error
Returns an error if the close operation fails.

GracefulClose

Closes the DataChannel gracefully and waits for any goroutines it started to complete.
error
Returns an error if the close operation fails.
This is only safe to call outside of DataChannel callbacks or if in a callback, in its own goroutine. Normally, close only stops writes, and graceful close will wait for reads to be finished based on underlying SCTP association closure or a SCTP reset stream from the other side.

Complete Example

SCTPTransport

The underlying SCTP transport for data channels

DataChannelMessage

Message structure containing data and type information

DataChannelParameters

Configuration parameters for creating data channels

DataChannelState

State enumeration for data channel lifecycle