This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
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.
RTCDataChannel.onmessage = function;
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.
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);
}
| Specification | Status | Comment |
|---|---|---|
| WebRTC 1.0: Real-time Communication Between Browsers The definition of 'RTCDataChannel.onmessage' in that specification. | Working Draft | Initial specification. |
| 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.
RTCPeerConnectionRTCDataChannelmessage event and its type, MessageEvent.
© 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/RTCDataChannel/onmessage