This is an experimental technology
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future versions of browsers as the specification changes.
The createDataChannel()
method on the RTCPeerConnection
interface creates a new channel 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 notificationneeded
event.
dataChannel = RTCPeerConnection.createDataChannel(label[, options]);
label
null
is treated as if you specified an empty string (""). This string may not be longer than 65,535 bytes.options
Optional
RTCDataChannelInit
dictionary providing configuration options for the data channelThe RTCDataChannelInit
dictionary provides the following fields, any of which may be included as needed in the object passed as the options parameter in order to configure the data channel to suit your needs:
ordered
Optional
RTCDataChannel
are required to arrive at their destination in the same order in which they were sent (true
), or if they're allowed to arrive out-of-order (false
). Default: true
.
maxPacketLifeTime
Optionalnull
.
maxRetransmits
Optional
null
.
protocol
Optional
RTCDataChannel
, if any; otherwise, the empty string (""). Default: empty string, ""
. This string may not be longer than 65,535 bytes.negotiated
Optional
true
), or if the channel was simply created by one of the peers, letting the other peer choose to accept the connection or not when it receives the resulting RTCDataChannelEvent
(false
). Default: false
.
id
Optional
The options which can be configured using the RTCDataChannelInit
dictionary represent the script-settable subset of the properties on the RTCDataChannel
interface.
A new 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
RTCPeerConnection
is closed.TypeError
SyntaxError
maxPacketLifeTime
and maxRetransmits
options. You may only specify a non-null
value for one of these.ResourceInUse
id
was specified, but another RTCDataChannel
is already using the same value.This simple example opens a channel and sets up handlers for the open
and message
events to send and receive messages on the data channel. For a more thorough example showing how the connection and channel are established, see A simple RTCDataChannel sample.
var pc = new RTCPeerConnection(); var channel = pc.createDataChannel("Mydata"); channel.onopen = function(event) { channel.send('sending a message'); } channel.onmessage = function(event) { console.log(event.data); }
Specification | Status | Comment |
---|---|---|
WebRTC 1.0: Real-time Communication Between Browser The definition of 'createDataChannel()' in that specification. | Working Draft | Initial definition. |
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
Basic support | (Yes) | 22 (22) | ? | ? | ? |
Feature | Android | Android Webview | Firefox Mobile (Gecko) | Firefox OS | IE Mobile | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | No support | No support | 24.0 (24) | ? [2] | ? | ? | ? | (Yes) |
© 2005–2017 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/createDataChannel