MDN self-hosted library · e01.org

RTCPeerConnectionStats

Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.

js
async function numberOpenConnections(peerConnection) {
  const stats = await peerConnection.getStats();
  let peerConnectionStats = null;

  for (const report of stats.values()) {
    if (report.type === "peer-connection") {
      peerConnectionStats = report;
      break;
    }
  }

  if (
    peerConnectionStats === null ||
    typeof peerConnectionStats.dataChannelsOpened === "undefined" ||
    typeof peerConnectionStats.dataChannelsClosed === "undefined"
  ) {
    return null;
  }

  return (
    peerConnectionStats.dataChannelsOpened -
    peerConnectionStats.dataChannelsClosed
  );
}