The stats API makes it easy to get information about your ongoing connections and other WebRTC internals. It closely follows the WebRTC Statistics API RFC. Note that the underlying WebRTC statistics API is not supported consistently from browser to browser. For instance Chrome exposes the most full-featured statistics whereas Edge does not implement it at all. In all cases where our statistics API is wrapping that provided by the browser, if the browser does not implement the given API then we simply return a null stats object. This is the expected behaviour and application code should be checking for null stats objects.
What follows is a code snippet showing how to get connection statistics:
You can call the
getStats function on any active
Connection. For example, one way to do so would be to wire up an event to call
getStats once the
Connection state transitions to
Connected, and the unwire the event when the Connection state transitions to
Failed). Your event could be triggered by some UI element (like a button click), or fired on a Timer, whatever meets your use case.