RTCPeerConnection: datachannel event

A datachannel event is sent to an RTCPeerConnection instance when an RTCDataChannel has been added to the connection, as a result of the remote peer calling RTCPeerConnection.createDataChannel().

Note: This event is not dispatched when the local end of the connection creates the channel.

Bubbles No
Cancelable No
Interface RTCDataChannelEvent
Event handler property ondatachannel

Examples

This example sets up a function that handles datachannel events by gathering the information needed to communicate with the newly added RTCDataChannel and by adding event handlers for the events that occur on that channel.

pc.addEventListener("datachannel", ev => {
  receiveChannel = ev.channel;
  receiveChannel.onmessage = myHandleMessage;
  receiveChannel.onopen = myHandleOpen;
  receiveChannel.onclose = myHandleClose;
}, false);

receiveChannel is set to the value of the event's channel property, which specifies the RTCDataChannel object representing the data channel linking the remote peer to the local one.

This same code can also instead use the RTCPeerConnection interface's ondatachannel event handler property, like this:

pc.ondatachannel = ev => {
  receiveChannel = ev.channel;
  receiveChannel.onmessage = myHandleMessage;
  receiveChannel.onopen = myHandleOpen;
  receiveChannel.onclose = myHandleClose;
}

Specifications

Specification Status Comment
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'datachannel' in that specification.
Candidate Recommendation Basic definition.

Browser compatibility

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
datachannel eventChrome Full support 25Edge Full support YesFirefox Full support 22IE ? Opera Full support 43Safari Full support 11WebView Android Full support YesChrome Android Full support 25Edge Mobile Full support YesFirefox Android Full support 44Opera Android Full support 43Safari iOS ? Samsung Internet Android Full support 6.0

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown

See also

Document Tags and Contributors

Contributors to this page: mdnwebdocs-bot, Sheppy, teoli
Last updated by: mdnwebdocs-bot,