W3cubDocs

/DOM

RTCPeerConnection.ondatachannel

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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 it.

Syntax

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

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

Browser compatibilityUpdate compatibility data on GitHub

Desktop
Chrome Edge Firefox Internet Explorer Opera Safari
Basic support 56 Yes 22 ? 43
43
Promise based version.
37 — 43
?
Mobile
Android webview Chrome for Android Edge Mobile Firefox for Android Opera for Android iOS Safari Samsung Internet
Basic support 56 56 Yes 44 43
43
Promise based version.
37 — 43
? 6.0

See also

© 2005–2018 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/ondatachannel