W3cubDocs

/DOM

RTCDataChannel.send

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 send() method of the RTCDataChannel interface sends data across the data channel to the remote peer. This can be done any time except during the initial process of creating the underlying transport channel. Data sent before connecting is buffered if possible (or an error occurs if it's not possible), and is also buffered if sent while the connection is closing or closed.

Syntax

RTCDataChannel.send(data);

Parameters

data
The data to transmit across the connection. This may be a USVString, a Blob, an ArrayBuffer, or an ArrayBufferView.

Exceptions

InvalidStateError
Since the data channel uses a separate transport channel from the media content, it must establish its own connection; if it hasn't finished doing so (that is, its readyState is "connecting"), this error occurs without sending or buffering the data.
NetworkError
The specified data would need to be buffered, and there isn't room for it in the buffer. In this scenario, the underlying transport is immediately closed.

Example

In this example, a routine called sendMessage() is created; it accepts an object as input and sends to the remote peer, over the RTCDataChannel, a JSON string with the specified object and a time stamp.

var pc = new RTCPeerConnection();
var dc = pc.createDataChannel("BackChannel");

function sendMessage(msg) {
  let obj = {
    "message": msg,
    "timestamp": new Date()
  }
  dc.send(JSON.stringify(obj));
}

Specifications

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

Browser compatibility

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

[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

© 2005–2017 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/send