RTCDataChannel

The RTCDataChannel interface represents a network channel which can be used for bidirectional peer-to-peer transfers of arbitrary data. Every data channel is associated with an RTCPeerConnection, and each peer connection can have up to a theoretical maximum of 65,534 data channels (the actual limit may vary from browser to browser).

To create a data channel and ask a remote peer to join you, call the RTCPeerConnection's createDataChannel() method. The peer being invited to exchange data receives a datachannel event (which has type RTCDataChannelEvent) to let it know the data channel has been added to the connection.

Properties

Also inherits properties from EventTarget.

binaryType

Is a string specifying the type of object that should be used to represent binary data received on the RTCDataChannel. Values are the same as allowed on the WebSocket.binaryType property: blob if Blob objects are being used, or arraybuffer if ArrayBuffer objects are being used. The default is blob.

bufferedAmount Read only

Returns the number of bytes of data currently queued to be sent over the data channel.

bufferedAmountLowThreshold

Specifies the number of bytes of buffered outgoing data that is considered "low". The default value is 0.

id Read only

Returns an ID number (between 0 and 65,534) which uniquely identifies the RTCDataChannel.

label Read only

Returns a string that contains a name describing the data channel. These labels are not required to be unique.

maxPacketLifeTime Read only

Returns the amount of time, in milliseconds, the browser is allowed to take to attempt to transmit a message, as set when the data channel was created, or null.

maxRetransmits Read only

Returns the maximum number of times the browser should try to retransmit a message before giving up, as set when the data channel was created, or null, which indicates that there is no maximum.

negotiated Read only

Indicates whether the RTCDataChannel's connection was negotiated by the Web app (true) or by the WebRTC layer (false). The default is false.

ordered Read only

Indicates whether or not the data channel guarantees in-order delivery of messages; the default is true, which indicates that the data channel is indeed ordered.

protocol Read only

Returns a string containing the name of the subprotocol in use. If no protocol was specified when the data channel was created, then this property's value is the empty string ("").

readyState Read only

Returns a string which indicates the state of the data channel's underlying data connection. It can have on of the following values: connecting, open, closing, or closed.

Event handlers

Also inherits event handlers from EventTarget.

onbufferedamountlow

Is an event handler which specifies a function the browser calls when thebufferedamountlow event is sent to the data channel.

onclose

Is an event handler which specifies a function to be called by the browser when the close event is received by the data channel.

onclosing

Is an event handler which specifies a function to be called by the browser when the closing event is received by the data channel.

onerror

Is an event handler which specifies a function to be called when the error event is received.

onmessage

Is an event handler which specifies a function to be called when the message event is fired on the channel.

onopen

Is an event handler which specifies a function to be called when the open event is fired on the

Obsolete properties

reliable Read only This is an obsolete API and is no longer guaranteed to work.

Indicates whether or not the data channel is reliable.

stream Read only This is an obsolete API and is no longer guaranteed to work.

Returns an ID number (between 0 and 65,535) which uniquely identifies the data channel.

Methods

Also inherits methods from EventTarget.

close()

Closes theRTCDataChannel. Either peer is permitted to call this method to initiate closure of the channel.

send()

Sends data across the data channel to the remote peer.

Events

bufferedamountlow

Sent when the number of bytes of data in the outgoing data buffer falls below the value specified by bufferedAmountLowThreshold. Also available as the onbufferedamountlow event handler property.

close

Sent when the underlying data transport closes. Also available as the onclose event handler property.

closing

Sent when the underlying data transport is about to start closing. Also available as the onclosing event handler property.

error

Sent when an error occurs on the data channel. Also available as the onerror event handler property.

message

Sent when a message has been received from the remote peer. The message contents can be found in the event's data property. Also available as the onmessage event handler property.

open

Sent when the data channel is first opened, or when an existing data channel's underlying connection re-opens. Also available as the onopen event handler property.

Data format

The underlying data format is defined by the IEEE draft specification draft-ietf-mmusic-sctp-sdp. The current format specifies its protocol as either "UDP/DTLS/SCTP" (UDP carrying DTLS carrying SCTP) or "TCP/DTLS/SCTP" (TCP carrying DTLS carrying SCTP). Older browsers may only specify "DTLS/SCTP".

Example

var pc = new RTCPeerConnection();
var dc = pc.createDataChannel("my channel");

dc.onmessage = function (event) {
  console.log("received: " + event.data);
};

dc.onopen = function () {
  console.log("datachannel open");
};

dc.onclose = function () {
  console.log("datachannel close");
};

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
RTCDataChannel
24
79
22
No
15
11
≤37
25
22
14
11
1.5
binaryType
24
79
22
No
15
11
≤37
25
22
14
11
1.5
bufferedAmount
24
79
22
No
15
11
≤37
25
22
14
11
1.5
bufferedamountlow_event
57
The default for rtcpMuxPolicy is require.
≤79
The default for rtcpMuxPolicy is require.
Yes
No
44
The default for rtcpMuxPolicy is require.
Yes
57
The default for rtcpMuxPolicy is require.
57
The default for rtcpMuxPolicy is require.
Yes
43
The default for rtcpMuxPolicy is require.
No
7.0
The default for rtcpMuxPolicy is require.
bufferedAmountLowThreshold
46
79
44
No
33
11
46
46
44
33
11
5.0
close
24
79
22
No
15
11
≤37
25
22
14
11
1.5
close_event
24
≤79
Yes
No
15
11
≤37
25
Yes
14
11
1.5
error_event
24
≤79
Yes
No
15
11
≤37
25
Yes
14
11
1.5
id
30
79
22
No
17
11
4.4
30
22
18
11
2.0
label
24
79
22
No
15
11
≤37
25
22
14
11
1.5
maxPacketLifeTime
56
79
62
No
43
11
56
56
62
43
11
6.0
maxRetransmits
30
79
62
No
17
11
4.4
30
62
18
11
2.0
message_event
24
≤79
Yes
No
15
Yes
≤37
25
Yes
14
Yes
1.5
negotiated
30
79
68
No
17
11
4.4
30
68
18
11
2.0
onbufferedamountlow
46
The default for rtcpMuxPolicy is require.
79
The default for rtcpMuxPolicy is require.
44
No
33
The default for rtcpMuxPolicy is require.
11
46
The default for rtcpMuxPolicy is require.
46
The default for rtcpMuxPolicy is require.
44
33
The default for rtcpMuxPolicy is require.
11
5.0
The default for rtcpMuxPolicy is require.
onclose
24
79
22
No
15
11
≤37
25
22
14
11
1.5
onclosing
81
81
No
No
68
No
81
81
No
58
No
13.0
onerror
24
79
22
No
15
11
≤37
25
22
14
11
1.5
onmessage
24
79
22
No
15
11
≤37
25
22
14
11
1.5
onopen
24
79
22
No
15
11
≤37
25
22
14
11
1.5
open_event
24
≤79
Yes
No
15
Yes
≤37
25
Yes
14
Yes
1.5
ordered
30
79
22
No
17
11
4.4
30
22
18
11
2.0
priority
No
No
No
No
No
15
No
No
No
No
15
No
protocol
30
79
22
No
17
11
4.4
30
22
18
11
2.0
readyState
24
79
22
No
15
11
≤37
25
22
14
11
1.5
reliable
24
79
22
No
15
No
≤37
25
22
14
No
1.5
sctp-sdp-21
58
≤79
63
No
?
No
58
58
63
?
No
7.0
send
24
79
22
No
15
11
≤37
25
22
14
11
1.5
stream
56
≤79
No
No
43
No
56
56
No
43
No
6.0

See also

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel