Since May 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
The RTCPeerConnectionStats dictionary of the WebRTC API provides information about the high level peer connection (RTCPeerConnection).
In particular, it provides the number of unique data channels that have been opened, and the number of opened channels that have been closed. This allows the current number of open channels to be calculated.
These statistics can be obtained by iterating the RTCStatsReport returned by RTCPeerConnection.getStats() until you find a report with the type of peer-connection.
dataChannelsOpenedA positive integer value indicating the number of unique RTCDataChannel objects that have entered the open state during their lifetime.
dataChannelsClosedA positive integer value indicating the number of unique RTCDataChannel objects that have left the open state during their lifetime (channels that transition to closing or closed without ever being open are not counted in this number). A channel will leave the open state if either end of the connection or the underlying transport is closed.
The following properties are common to all WebRTC statistics objects.
idA string that uniquely identifies the object that is being monitored to produce this set of statistics.
timestampA DOMHighResTimeStamp object indicating the time at which the sample was taken for this statistics object.
typeA string with the value "peer-connection", indicating the type of statistics that the object contains.
This example shows a function to return the total number of open connections, or null if no statistics are provided. This might be called in a loop, similar to the approach used in RTCPeerConnection.getStats() example
The function waits for the result of a call to RTCPeerConnection.getStats() and then iterates the returned RTCStatsReport to get just the stats of type "peer-connection". It then returns the total number of open channels, or null, using the data in the report.
async function numberOpenConnections (peerConnection) {
const stats = await peerConnection.getStats();
let peerConnectionStats = null;
stats.forEach((report) => {
if (report.type === "peer-connection") {
peerConnectionStats = report;
break;
}
});
result = (typeof peerConnectionStats.dataChannelsOpened === 'undefined' || typeof peerConnectionStats.dataChannelsClosed=== 'undefined') ? null : peerConnectionStats.dataChannelsOpened - peerConnectionStats.dataChannelsClosed;
return result
}
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
RTCPeerConnectionStats |
80 | 80 | 113 | 67 | 13.1 | 80 | 113 | 57 | 13.4 | 13.0 | 80 | 13.4 |
dataChannelsClosed |
80 | 80 | 113 | 67 | 13.1 | 80 | 113 | 57 | 13.4 | 13.0 | 80 | 13.4 |
dataChannelsOpened |
80 | 80 | 113 | 67 | 13.1 | 80 | 113 | 57 | 13.4 | 13.0 | 80 | 13.4 |
id |
80 | 80 | 113 | 67 | 13.1 | 80 | 113 | 57 | 13.4 | 13.0 | 80 | 13.4 |
timestamp |
80 | 80 | 113 | 67 | 13.1 | 80 | 113 | 57 | 13.4 | 13.0 | 80 | 13.4 |
type |
80 | 80 | 113 | 67 | 13.1 | 80 | 113 | 57 | 13.4 | 13.0 | 80 | 13.4 |
© 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/RTCPeerConnectionStats