WebRTC Adapter
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
-
ReferenceError: adapter is not defined
cause Attempting to access the `adapter` object without properly importing it in a module environment or before its script tag has executed in a global environment.fixIf using a module system (ESM/CommonJS), ensure you have `import adapter from 'webrtc-adapter';` or `const adapter = require('webrtc-adapter');` at the top of your file. If using a `<script>` tag, ensure the adapter script is loaded before your code that references `adapter`. -
TypeError: navigator.mediaDevices is undefined
cause Accessing `navigator.mediaDevices` in an older browser or a browser context where it's not yet defined, or when the `webrtc-adapter` shim has not been properly applied.fixEnsure `webrtc-adapter` is loaded and executed *before* any WebRTC API calls, especially `navigator.mediaDevices.getUserMedia`. The adapter polyfills `navigator.mediaDevices` for older browsers or contexts where it might be missing or prefixed. Also, check if the browser fundamentally supports WebRTC.
Warnings
- gotcha The package `webrtc-adapter-test` is an internal or deprecated testing package (version 0.2.10). For production use and active development, install and use the `webrtc-adapter` package (latest is 9.0.4).
- breaking Major version updates of `webrtc-adapter` can introduce breaking changes, such as the removal of legacy shims (e.g., `navigator.getDisplayMedia` shim was removed) or modifications to API function signatures (`getStats`). These changes align with WebRTC specification evolution, but can break older applications.
- gotcha When bundling for the browser, be aware of the different output files: `adapter.js` exposes the `adapter` object globally on `window.adapter`, while `adapter_no_global.js` does not. Choose the correct file based on whether you expect a global object or plan to import it as a module.
- deprecated Direct browser prefix usage (e.g., `navigator.webkitGetUserMedia`) is deprecated. While the adapter historically handled these, modern WebRTC development should target the standard `navigator.mediaDevices.getUserMedia` and `RTCPeerConnection` APIs, letting the adapter manage subtle browser differences.
Install
-
npm install webrtc-adapter-test -
yarn add webrtc-adapter-test -
pnpm add webrtc-adapter-test
Imports
- adapter (global)
import adapter from 'webrtc-adapter'; // when using script tag
<!-- included via script tag -->
- adapter (ESM)
const adapter = require('webrtc-adapter'); // For modern browser builds (ESM)import adapter from 'webrtc-adapter';
- adapter (CJS)
import adapter from 'webrtc-adapter'; // In CommonJS environments (Node.js/older bundlers)
const adapter = require('webrtc-adapter'); - adapter.browserDetails
console.log(window.browserDetails.browser); // Not a direct global
console.log(adapter.browserDetails.browser);
Quickstart
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();