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 🙏

© 2025 – Pkg Stats / Ryan Hefner

mediamtx-webrtc-react

v0.1.0

Published

TypeScript/React-compatible WebRTC reader for MediaMTX WHEP streams

Readme

MediaMTX WebRTC Reader - TypeScript/React

NPM Version NPM Downloads

A TypeScript-compatible WebRTC reader for MediaMTX WHEP streams with React integration.

This is a close port of the MediaMTX's reader.js (based on v1.15.2).

Installation

pnpm add mediamtx-webrtc-react
# or
npm install mediamtx-webrtc-react
# or
yarn add mediamtx-webrtc-react

Usage

1. Direct TypeScript Class (Same API as original)

import { MediaMTXWebRTCReader } from 'mediamtx-webrtc-react';

const reader = new MediaMTXWebRTCReader({
  url: "http://mediamtx-ip:8889/mystream/whep",
  user: "", // optional
  pass: "", // optional
  token: "", // optional
  onError: (err) => {
    console.error(err);
  },
  onTrack: (evt) => {
    const videoElement = document.getElementById("myvideo") as HTMLVideoElement;
    videoElement.srcObject = evt.streams[0];
  },
});

// Clean up when done
// reader.close();

2. React Hook

import React from 'react';
import { useMediaMTXWebRTC } from 'mediamtx-webrtc-react';

function VideoPlayer() {
  const { 
    videoRef, 
    connectionState, 
    error, 
    isConnecting,
    isConnected,
    stream 
  } = useMediaMTXWebRTC({
    url: "http://mediamtx-ip:8889/mystream/whep",
    onError: (err) => console.error(err)
  });

  return (
    <div>
      <video ref={videoRef} autoPlay controls />
      <p>Status: {connectionState}</p>
      {error && <p>Error: {error}</p>}
    </div>
  );
}

3. Simple React Component

import React from 'react';
import { WebRTCVideo } from 'mediamtx-webrtc-react';

function App() {
  return (
    <WebRTCVideo 
      url="http://mediamtx-ip:8889/mystream/whep"
      onError={(err) => console.error(err)}
      onConnectionStateChange={(state) => console.log('State:', state)}
      autoPlay
      controls
      style={{ width: '100%', maxWidth: '800px' }}
    />
  );
}

4. Audio Streaming

import React from 'react';
import { WebRTCAudio } from 'mediamtx-webrtc-react';

function AudioPlayer() {
  return (
    <WebRTCAudio 
      url="http://mediamtx-ip:8889/audiostream/whep"
      controls
    />
  );
}

Migration from JavaScript

The TypeScript version maintains full backward compatibility with the original JavaScript API:

Before (JavaScript):

<script src="reader.js"></script>
<script>
  let reader = new MediaMTXWebRTCReader({
    url: "http://mediamtx-ip:8889/mystream/whep",
    onTrack: (evt) => {
      document.getElementById("myvideo").srcObject = evt.streams[0];
    },
  });
</script>

After (TypeScript):

import { MediaMTXWebRTCReader } from 'mediamtx-webrtc-react';

const reader = new MediaMTXWebRTCReader({
  url: "http://mediamtx-ip:8889/mystream/whep",
  onTrack: (evt) => {
    const video = document.getElementById("myvideo") as HTMLVideoElement;
    video.srcObject = evt.streams[0];
  },
});

API Reference

MediaMTXWebRTCReaderConfig

interface MediaMTXWebRTCReaderConfig {
  url: string;           // WHEP endpoint URL
  user?: string;         // Optional username
  pass?: string;         // Optional password  
  token?: string;        // Optional bearer token
  onError?: (err: string) => void;
  onTrack?: (evt: RTCTrackEvent) => void;
}

useMediaMTXWebRTC Hook

const {
  // Connection state
  connectionState,     // 'getting_codecs' | 'running' | 'restarting' | 'closed' | 'failed'
  isConnecting,        // boolean
  isConnected,         // boolean
  error,              // string | null
  stream,             // MediaStream | null
  retryCount,         // number
  lastConnectedAt,    // Date | null
  
  // Element refs
  videoRef,           // RefObject<HTMLVideoElement>
  audioRef,           // RefObject<HTMLAudioElement>
  
  // Control methods
  reader,             // MediaMTXWebRTCReader instance
  close,              // () => void
  restart,            // () => void
} = useMediaMTXWebRTC(config);

Features

  • Full TypeScript support with strict typing
  • React hooks for easy integration
  • Automatic video/audio element handling
  • Connection state management
  • Error handling and retry logic
  • Backward compatibility with original JavaScript API
  • ESM/CommonJS support
  • Tree-shakeable exports

Browser Support

  • Chrome/Chromium 80+
  • Firefox 74+
  • Safari 13.1+
  • Edge 80+