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
.
The following properties are common to all WebRTC statistics objects.
id
-
A string that uniquely identifies the object that is being monitored to produce this set of statistics.
timestamp
-
A DOMHighResTimeStamp
object indicating the time at which the sample was taken for this statistics object.
type
-
A 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
}