Overview
TheDataChannel 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
Creating a DataChannel
NewDataChannel
*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
ID
ReadyState
DataChannelState
One of:
DataChannelStateConnecting, DataChannelStateOpen, DataChannelStateClosing, or DataChannelStateClosed.Protocol
Ordered
true if the DataChannel is ordered, and false if out-of-order delivery is allowed.
MaxPacketLifeTime
MaxRetransmits
Negotiated
true), or not (false).
Transport
Sending Data
Send
[]byte
required
The binary data to send.
error
Returns an error if the channel is not open or if the write fails.
SendText
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
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
func()
required
The callback function to invoke when dialing begins.
OnMessage
func(msg DataChannelMessage)
required
The callback function to invoke when a message is received.
OnClose
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
func(err error)
required
The callback function to invoke when an error occurs.
Buffered Amount
BufferedAmount
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
OnBufferedAmountLow event fires.
The threshold is initially zero on each new DataChannel, but the application may change its value at any time.
SetBufferedAmountLowThreshold
uint64
required
The new threshold value in bytes.
OnBufferedAmountLow
func()
required
The callback function to invoke when the buffered amount becomes low.
Advanced Operations
Detach
io.ReadWriteCloser API with .Read() and .Write() methods instead of .Send() and .OnMessage().
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
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
error
Returns an error if the close operation fails.
GracefulClose
error
Returns an error if the close operation fails.
Complete Example
Related Types
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