RTCDataChannel.onmessage

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 RTCDataChannel.ommessage property stores an EventHandler which specifies a function to be called when the message event is fired on the channel. This event is represented by the MessageEvent interface. This event is sent to the channel when a message is received from the other peer.

Syntax

RTCDataChannel.onmessage = function;

Value

A function which the browser will call to handle the message event. The function receives as its sole input parameter a MessageEvent object describing the event.

Example

This code snippet creates a peer connection, adds a data channel to it, and starts creating new <p> (paragraph) elements each time a message arrives, with the message's contents displayed inside it. The new elements are then attached to the end of the document.

let pc = new RTCPeerConnection();
let dc = pc.createDataChannel();
dc.onmessage = function(event) {
  var el = document.createElement("p");
  var txtNode = document.createTextNode(event.data);
  el.appendChild(txtNode);
  receiveBox.appendChild(el);
}

Specifications

Specification Status Comment
WebRTC 1.0: Real-time Communication Between Browsers
The definition of 'RTCDataChannel.onmessage' in that specification.
Working Draft Initial specification.

Browser compatibility

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

[1] The interface is called DataChannel and not RTCDataChannel in Firefox. However, a binding has been in place since Firefox 24 so that either name will work.

See also

Document Tags and Contributors

 Contributors to this page: jpmedley, Sheppy, teoli
 Last updated by: jpmedley,