← View all posts
February 25, 2017

Fiddle-of-the-week: Spec-compliant getStats

Contributed by Jan-Ivar Bruaroey,

Firefox has supported getStats to the spec for a couple of years now, and Chrome Canary finally has a spec-compliant version, so it’s a good time to check in how to write code that (soon!) will work across browsers.

Unfortunately, the specification has changed several times over the years, even breaking Firefox. Here’s how to tell whether your getStats code is up to spec as of 2017:

  1. It uses promises (the callback version was removed from the spec!)
  2. You iterate on a maplike stats report object.
  3. Type fields have hyphens. E.g. "inbound-rtp", NOT "inboundrtp".
  4. No goog or moz prefixes.

Oh, goodness, I hear you say. adapter.js to the rescue. Here’s an example that works in Firefox and Chrome Canary:

Click the “Result” tab above to see it run. Tick the “Mute” checkbox and see if your browser stops sending video packets!

There’s still a lot of work to do. Firefox supports only a subset of what the spec mandates. The point of this blog however, is to help you write spec-compliant code, so we all converge on the spec, for a healthier web.