npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@flashphoner/web-sdk-metrics

v1.0.10

Published

Official Flashphoner RTC metrics collector

Downloads

140

Readme

RTCMetricsCollector

RTCMetricsCollector is a component for collecting, batching, compressing, and sending WebRTC statistics from an RTCPeerConnection to a backend service according to a server-defined contract.

The class encapsulates all low-level logic required to:

  • read WebRTC statistics via getStats()
  • extract and track metric headers
  • batch and deduplicate metric values
  • compress metric payloads
  • send data over WebSocket or HTTP
  • handle errors and stop collection when limits are exceeded

This class is intended to be used as part of a WebRTC SDK or metrics pipeline.


Purpose

RTCMetricsCollector is designed to solve the following problems:

  • Periodic collection of WebRTC statistics
  • Strict adherence to a backend-defined metrics schema
  • Efficient network usage through batching, deduplication, and compression
  • Automatic handling of changes in RTCStats structure during a session
  • Decoupling metrics collection from transport implementation

The collector does not know how metrics are transported. Transport is delegated to an implementation of IRTCMetricsSender.


High-Level Architecture

RTCPeerConnection
↓ getStats()
RTCMetricsCollector

RTCMetricsBatch → Compressor

IRTCMetricsSender (WebSocket or HTTP)


Key Features

  • Collection of metrics by RTCStats type (codec, transport, outbound-rtp, etc.)
  • Automatic extraction and tracking of metric headers
  • Re-sending headers when stats structure changes
  • Fixed-size batching of metrics
  • Deduplication of repeated values between batch rows
  • Optional compression (none or gzip)
  • Custom report filters
  • Automatic shutdown after exceeding error threshold

Basic Usage

const collector = new RTCMetricsCollector(
    "media-session-id",
    serverDescription,
    peerConnection,
    sender,
    logger,
    3
);

await collector.start();

Constructor

new RTCMetricsCollector(
    id
:
string,
    description
:
RTCMetricsServerDescription,
    peerConnection
:
RTCPeerConnection,
    sender
:
IRTCMetricsSender,
    logger
:
ILogger,
    maxErrors
:
number
)

Parameters

| Parameter | Description | |----------------|------------------------------------| | id | Unique media session identifier | | description | Server-defined metrics description | | peerConnection | WebRTC peer connection | | sender | Metrics transport implementation | | logger | Logger instance | | maxErrors | Maximum allowed send errors |


Lifecycle

start()

await collector.start();
  • Validates configuration
  • Extracts and sends initial metric headers
  • Starts periodic collection if enabled in description

stop()

collector.stop();
  • Stops periodic sampling
  • Resets internal state

Enable / Disable Collection

collector.collect(true);
collector.collect(false);

Filters

Custom filters can be added to exclude specific RTCStats reports:

collector.addStatsReportFilter({
    allowed(report) {
        return report.kind !== "video";
    }
});

If any filter returns false, the report is ignored.


Headers Handling

The collector dynamically builds a list of metric headers from RTCStats reports.

If the header structure changes:

  • the current batch is flushed
  • updated headers are sent to the backend

This guarantees that the backend can always correctly interpret incoming metric batches.


Batching and Deduplication

  • Metrics are accumulated into a RTCMetricsBatch
  • When batchSize is reached, the batch is sent
  • Duplicate values compared to the previous row are replaced with empty strings
  • This significantly reduces payload size

Compression

Compression type is defined by the server description:

  • none
  • gzip

Compression is applied automatically before sending a batch.


Error Handling

  • Failed send attempts increment an internal error counter
  • When the counter exceeds maxErrors, collection is stopped automatically

When to Use

Use RTCMetricsCollector when you need:

  • Continuous WebRTC statistics collection
  • Efficient, low-overhead metrics transport
  • Dynamic handling of stats structure changes
  • A clean separation between metrics logic and transport

When Not to Use

RTCMetricsCollector may be unnecessary if:

  • You only need a one-time getStats() call
  • Metrics are not sent to a backend
  • Batching and compression are not required

In such cases, direct usage of RTCPeerConnection.getStats() may be sufficient.


Related Components

  • RTCMetricsCollectorBuilder — helper for constructing collectors
  • RTCMetricsBatch — batch storage and deduplication
  • IRTCMetricsSender — metrics transport interface
  • RTCMetricsServerDescription — server-side metrics contract