files/en-us/web/api/rtcdatachannel/index.md
{{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}}
Also inherits properties from {{DOMxRef("EventTarget")}}.
RTCDataChannel.
Values are the same as allowed on the {{DOMxRef("WebSocket.binaryType")}} property:
blob if {{DOMxRef("Blob")}} objects are being used,
or arraybuffer if {{jsxref("ArrayBuffer")}} objects are being used.
The default is arraybuffer.RTCDataChannel.null.null, which indicates that there is no maximum.RTCDataChannel's connection was negotiated by the Web app
(true)
or by the WebRTC layer (false).
The default is false.true, which indicates that the data channel is indeed ordered."very-low", "low", "medium", or "high"."").connecting, open, closing, or closed.Also inherits methods from {{DOMxRef("EventTarget")}}.
RTCDataChannel.
Either peer is permitted to call this method
to initiate closure of the channel.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".
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}}
{{Compat}}