RTCDataChannel.close()

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.close() method closes the RTCDataChannel. Either peer is permitted to call this method to initiate closure of the channel.

Closure of the data channel is not instantaneous. Most of the process of closing the connection is handled asynchronously; you can detect when the channel has finished closing by watching for a close event on the data channel.

The sequence of events which occurs in response to this method being called:

  1. RTCDataChannel.readyState is set to "closing".
  2. A background task is established to handle the remainder of the steps below, and close() returns to the caller.
  3. The transport layer deals with any buffered messages; the protocol layer decides whether to send them or discard them.
  4. The underlying data transport is closed.
  5. The RTCDataChannel.readyState property is set to "closed".
  6. If the transport was closed with an error, the RTCDataChannel is sent a NetworkError event.
  7. A close event is sent to the channel.

In Firefox, the RTCDataChannel interface was implemented under the name DataChannel until Firefox 24, so this method was called DataChannel.close().

Syntax

JavaScript
RTCDataChannel.close();

This method has no input parameters, and returns nothing.

Example

JavaScript
var pc = new RTCPeerConnection();
var dc = pc.createDataChannel("my channel");

dc.onmessage = function (event) {
  console.log("received: " + event.data);
  dc.close(); // We decided to close after the first received message
};

dc.onopen = function () {
  console.log("datachannel open");
};

dc.onclose = function (
  console.log("datachannel close");
};

// Now negotiate the connection and so forth...

Specifications

Specification Status Comment
WebRTC 1.0: Real-time Communication Between Browser
The definition of 'RTCDataChannel.close()' 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 Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support ? 28 [2] 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.

[2] WebRTC was available behind a flag in Chrome 28 for Android, and was enabled by default starting in Chrome 29.

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/rtcdatachannel/close

close Experimental Method Reference RTCDataChannel WebRTC