WebRTC Adapter

0.2.10 · active · verified Tue Apr 21

The WebRTC Adapter, often referred to as `adapter.js`, is a critical JavaScript shim library designed to mitigate cross-browser inconsistencies and insulate applications from evolving WebRTC API specifications. While initial WebRTC implementations featured significant vendor prefixes (e.g., `webkitGetUserMedia`, `mozRTCPeerConnection`), these have largely converged to standard names. However, subtle behavioral differences and API quirks still persist across browsers like Chrome, Firefox, and Safari. The adapter transparently intercepts and normalizes these variations, allowing developers to write more consistent and future-proof WebRTC code without extensive browser-specific conditionals or polyfills. Currently, the actively maintained version is `9.0.4`, available via `webrtc-adapter` on npm. Maintained by WebRTC experts (under `webrtcHacks` on GitHub), it offers a stable and reliable solution for ensuring WebRTC applications function seamlessly across diverse environments. Its release cadence typically follows browser updates and WebRTC specification adjustments to maintain compatibility.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import and use the WebRTC Adapter, then acquire and display a local video and audio stream using `navigator.mediaDevices.getUserMedia` which the adapter may have shimmed. It also logs browser details detected by the adapter.

import adapter from 'webrtc-adapter';

const localVideo = document.createElement('video');
localVideo.autoplay = true;
localVideo.muted = true;
document.body.appendChild(localVideo);

console.log('WebRTC Adapter loaded. Browser:', adapter.browserDetails.browser, 'Version:', adapter.browserDetails.version);

const startMedia = async () => {
  try {
    const stream = await navigator.mediaDevices.getUserMedia({
      video: true,
      audio: true
    });
    localVideo.srcObject = stream;
    console.log('Local stream acquired and displayed.');
  } catch (e) {
    console.error('Error accessing media devices:', e);
    alert(`Error accessing media: ${e.name} - ${e.message}`);
  }
};

startMedia();

view raw JSON →