docs.rodeo

MDN Web Docs mirror

RTCDataChannel

{{APIRef("WebRTC")}} 

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 {{DOMxRef("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 {{DOMxRef("RTCPeerConnection")}} 's {{DOMxRef("RTCPeerConnection.createDataChannel", "createDataChannel()")}}  method. The peer being invited to exchange data receives a {{DOMxRef("RTCPeerConnection.datachannel_event", "datachannel")}}  event (which has type {{DOMxRef("RTCDataChannelEvent")}} ) to let it know the data channel has been added to the connection.

RTCDataChannel is a transferable object.

{{InheritanceDiagram}} 

Instance properties

Also inherits properties from {{DOMxRef("EventTarget")}} .

Obsolete properties

Instance methods

Also inherits methods from {{DOMxRef("EventTarget")}} .

Events

Data format

The underlying data format is defined by the IEEE specification SDP Offer/Answer Procedures for SCTP over DTLS Transport(RFC 8841). 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

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

dc.onmessage = (event) => {
  console.log(`received: ${event.data}`);
};

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

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

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN