files/en-us/web/api/rtcpeerconnection/createdatachannel/index.md
{{APIRef("WebRTC")}}
The createDataChannel() method of the {{domxref("RTCPeerConnection")}} interface creates a new channel linked with the remote peer, over which any kind of data may be transmitted.
This can be useful for back-channel content, such as images, file transfer, text chat, game update packets, and so forth.
If the new data channel is the first one added to the connection, renegotiation is started by delivering a {{DOMxRef("RTCPeerConnection/negotiationneeded_event", "negotiationneeded")}} event.
createDataChannel(label)
createDataChannel(label, options)
label
options {{optional_inline}}
ordered {{optional_inline}}
true), or if they're allowed to arrive out-of-order (false).
Default: true.maxPacketLifeTime {{optional_inline}}
null.maxRetransmits {{optional_inline}}
null.protocol {{optional_inline}}
"").
Default: empty string ("").
This string may not be longer than 65,535 bytes.negotiated {{optional_inline}}
false), data channels are negotiated in-band, where one side calls createDataChannel, and the other side listens to the {{domxref("RTCDataChannelEvent")}} event using the {{DOMxRef("RTCPeerConnection.datachannel_event", "ondatachannel")}} event handler.
Alternatively (true), they can be negotiated out of-band, where both sides call createDataChannel with an agreed-upon ID.
Default: false.id {{optional_inline}}
[!NOTE] These options represent the script-settable subset of the properties on the {{domxref("RTCDataChannel")}} interface.
A new {{domxref("RTCDataChannel")}} object with the specified label, configured using the options specified by options if that parameter is included; otherwise, the defaults listed above are established.
InvalidStateError {{domxref("DOMException")}}
id is 65535. While this is a valid unsigned 16-bit value, it's not a permitted value for id.SyntaxError {{domxref("DOMException")}}
maxPacketLifeTime and maxRetransmits options.
You may specify a non-null value for only one of these.ResourceInUse {{domxref("DOMException")}}
id was specified, but another {{domxref("RTCDataChannel")}} is already using the same value.OperationError {{domxref("DOMException")}}
id is already in use, or if no id was specified, the WebRTC layer was unable to automatically generate an ID because all IDs are in use.This example shows how to create a data channel and set up handlers for the {{DOMxRef("RTCDataChannel/open_event", "open")}} and {{DOMxRef("RTCDataChannel/message_event", "message")}} events to send and receive messages on it (For brevity, the example assumes onnegotiationneeded is set up).
// Offerer side
const pc = new RTCPeerConnection(options);
const channel = pc.createDataChannel("chat");
channel.onopen = (event) => {
channel.send("Hi you!");
};
channel.onmessage = (event) => {
console.log(event.data);
};
// Answerer side
const pc = new RTCPeerConnection(options);
pc.ondatachannel = (event) => {
const channel = event.channel;
channel.onopen = (event) => {
channel.send("Hi back!");
};
channel.onmessage = (event) => {
console.log(event.data);
};
};
Alternatively, more symmetrical out-of-band negotiation can be used, using an agreed-upon id (0 here):
// Both sides
const pc = new RTCPeerConnection(options);
const channel = pc.createDataChannel("chat", { negotiated: true, id: 0 });
channel.onopen = (event) => {
channel.send("Hi!");
};
channel.onmessage = (event) => {
console.log(event.data);
};
For a more thorough example showing how the connection and channel are established, see A simple RTCDataChannel sample.
{{Specifications}}
{{Compat}}