RTCPeerConnection.ondatachannel

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 RTCPeerConnection.ondatachannel property is an EventHandler which specifies a function which is called when the datachannel event occurs on an RTCPeerConnection. This event, of type RTCDataChannelEvent, is sent when an RTCDataChannel is added to the connection by the remote peer calling createDataChannel().

At the time this event is received, the RTCDataChannel it indicates may not yet actually be open. Be sure to wait for the "open" event to be fired on the new RTCDataChannel before using the it.

Syntax

JavaScript
RTCPeerConnection.ondatachannel = function;

Value

Set this property to be a function you provide which receives as input a single parameter: an RTCDataChannelEvent which provides in its channel property the RTCDataChannel which has been created.

Example

JavaScript
pc.ondatachannel = function(ev) {
  console.log('Data channel is created!');
  ev.channel.onopen = function() {
    console.log('Data channel is open and ready to be used.');
  };
};

Specifications

Specification Status Comment
WebRTC 1.0: Real-time Communication Between Browser
The definition of 'RTCPeerConnection.ondatachannel' in that specification.
Working Draft Initial specification.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) [1] 18 (18) [2] No support (Yes) ?
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? ? 22.0 (22) [2] No support ? ?

[1] Though this property is not prefixed, the interface it belongs to is.

[2] This property's name isn't prefixed, but the interface it's in, RTCPeerConnection, was prefixed as MozRTCPeerConnection until Firefox 44.

See also

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/api/rtcpeerconnection/ondatachannel

Event Handler Experimental ondatachannel Property Reference RTCPeerConnection WebRTC