Path: blob/main/files/en-us/web/api/rtcpeerconnection/getstats/index.md
4417 views
---
---
{{APIRef("WebRTC")}}
The {{domxref("RTCPeerConnection")}} method getStats()
returns a promise which resolves with data providing statistics about either the overall connection or about the specified {{domxref("MediaStreamTrack")}}.
Syntax
Parameters
selector
{{optional_inline}}: A {{domxref("MediaStreamTrack")}} for which to gather statistics. If this is
null
(the default value), statistics will be gathered for the entire {{domxref("RTCPeerConnection")}}.
Deprecated parameters
In older code and documentation, you may see a callback-based version of this function. This has been deprecated and its use is strongly discouraged. You should update any existing code to use the {{jsxref("Promise")}}-based version of getStats()
instead. The parameters for the older form of getStats()
are described below, to aid in updating existing code.
successCallback
{{deprecated_inline}}: A callback function called once the report has been successfully generated.
failureCallback
{{deprecated_inline}}: A callback function called once the report has failed to be generated.
Return value
A {{jsxref("Promise")}} which resolves with an {{domxref("RTCStatsReport")}} object providing connection statistics. The report's contents depend on the selector
and other details of the connection.
Exceptions
This method does not throw exceptions; instead, it rejects the returned promise with one of the following errors:
InvalidAccessError
{{domxref("DOMException")}}: Thrown when there is no {{domxref("RTCRtpSender")}} or {{domxref("RTCRtpReceiver")}} whose
track
matches the specifiedselector
, orselector
matches more than one sender or receiver.
Examples
This example creates a periodic function using {{domxref("setInterval()")}} that collects statistics for an {{domxref("RTCPeerConnection")}} every second, generating an HTML-formatted report and inserting it into a specific element in the DOM.
This works by calling getStats()
, then, when the promise is resolved, iterates over the {{domxref("RTCStats")}} objects on the returned {{domxref("RTCStatsReport")}}. A section is created for each report with a header and all of the statistics below, with the type, ID, and timestamp handled specially to place them at the top of the list.
Once the HTML for the report is generated, it is injected into the element whose class is "stats-box"
by setting its {{domxref("Element.innerHTML", "innerHTML")}} property.
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}