W3cubDocs

/Web APIs

RTCPeerConnection: close() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨September 2017⁩.

The close() method of the RTCPeerConnection interface closes the current peer connection.

Calling this method terminates the RTCPeerConnection's ICE agent, ending any ongoing ICE processing and any active streams. This also releases any resources in use by the ICE agent, including TURN permissions. All RTCRtpSender objects are considered to be stopped once this returns (they may still be in the process of stopping, but for all intents and purposes, they're stopped).

Once this method returns, the signaling state as returned by RTCPeerConnection.signalingState is closed.

Make sure that you delete all references to the previous RTCPeerConnection before attempting to create a new one that connects to the same remote peer, as not doing so might result in some errors depending on the browser.

Syntax

close()

Parameters

None.

Return value

None (undefined).

Example

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

dc.onmessage = (event) => {
  console.log(`received: ${event.data}`);
  pc.close(); // We decided to close after the first received message
};

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

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

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Opera Safari Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet WebView Android WebView on iOS
close 23 15 22 15 11 25 24 14 11 1.5 4.4 11

See also

© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/close