peerjs is a free, open source browsers & extensions project written in TypeScript and released under MIT. It has 13,446 GitHub stars, 1,460 forks and 201 open issues, and was last pushed 7 months ago. On this registry it ranks #32 of 65 tracked projects in Browsers & Extensions, with 5 head-to-head comparisons available.

What is peerjs?

What it is

PeerJS is an open-source library that provides a complete, configurable, and easy-to-use peer-to-peer API built on top of WebRTC. It lives in the JavaScript and TypeScript ecosystem, is written in TypeScript, and is distributed under the MIT license. The library supports both data channels and media streams, so applications can send arbitrary messages between browsers or negotiate audio and video calls without routing traffic through a central media server.

The concrete problem PeerJS solves is the complexity of raw WebRTC. Establishing a peer connection by hand requires signalling, session description negotiation, ICE candidate exchange, and careful browser-specific handling. PeerJS wraps that machinery behind a small API: a developer creates a Peer with an optional identifier, calls peer.connect for a data connection or peer.call for a media call, and listens for connection, data, call, and stream events. A companion project, PeerServer, handles the signalling broker that peers use to find one another.

Key capabilities

  • Data connections between peers through peer.connect, with conn.send and a data event for receiving messages.
  • Media calls through peer.call and call.answer, carrying audio and video streams obtained from navigator.mediaDevices.getUserMedia.
  • Configurable peer identifiers, since a Peer can be constructed with a chosen id or left to receive a random one from the server.
  • Support for CBOR and MessagePack serialization, which requires Firefox 102 or newer.
  • Official browser support for Firefox 80+, Chrome 83+, Edge 83+, and Safari 15+, tested with BrowserStack.
  • A documented API reference and a live example application that demonstrates both media and data connections.
  • Distribution through npm and yarn as the peerjs package.

Who uses it and how

  • Browser applications that need direct data exchange between clients, using the data connection API to send and receive messages.
  • Video and audio calling features embedded in web apps, where the media call API negotiates streams between two peers.
  • Developers following the live Glitch example, which combines media and data connections and runs against its own PeerServer instance.
  • Teams that self-host the signalling layer by deploying PeerServer alongside their client application.
  • Projects building on the Parcel and Webpack toolchains, with the caveat that Webpack emits a non-critical "request of a dependency is an expression" warning related to Parcel.

Getting started

Install with npm install peerjs or yarn add peerjs, then import Peer and construct an instance with an id or without one. Tests run with npm test, and a hosted PeerServer is available for signalling.

When to use it — and when not to

PeerJS is a strong fit when a project needs WebRTC peer connections without writing signalling and negotiation code by hand, and it remains actively maintained after fourteen years. A self-hoster must operate a PeerServer instance for signalling, which is a separate repository from the client library. The library carries 201 open issues and officially supports only recent browser versions, so older browsers and unsupported environments are outside its tested scope

project readme (upstream, from github) — read inline

PeerJS: Simple peer-to-peer with WebRTC

Backers on Open Collective Sponsors on Open Collective Discord

PeerJS provides a complete, configurable, and easy-to-use peer-to-peer API built on top of WebRTC, supporting both data channels and media streams.

Live Example

Here's an example application that uses both media and data connections: https://glitch.com/~peerjs-video. The example also uses its own PeerServer.


Special Announcement:

We now have a Discord Channel
There we plan to discuss roadmaps, feature requests, and more
Join us today


Setup

Include the library

with npm: npm install peerjs

with yarn: yarn add peerjs

// The usage -
import { Peer } from "peerjs";

Create a Peer

const peer = new Peer("pick-an-id");
// You can pick your own id or omit the id if you want to get a random one from the server.

Data connections

Connect

const conn = peer.connect("another-peers-id");
conn.on("open", () => {
	conn.send("hi!");
});

Receive

peer.on("connection", (conn) => {
	conn.on("data", (data) => {
		// Will print 'hi!'
		console.log(data);
	});
	conn.on("open", () => {
		conn.send("hello!");
	});
});

Media calls

Call

navigator.mediaDevices.getUserMedia(
	{ video: true, audio: true },
	(stream) => {
		const call = peer.call("another-peers-id", stream);
		call.on("stream", (remoteStream) => {
			// Show stream in some <video> element.
		});
	},
	(err) => {
		console.error("Failed to get local stream", err);
	},
);

Answer

peer.on("call", (call) => {
	navigator.mediaDevices.getUserMedia(
		{ video: true, audio: true },
		(stream) => {
			call.answer(stream); // Answer the call with an A/V stream.
			call.on("stream", (remoteStream) => {
				// Show stream in some <video> element.
			});
		},
		(err) => {
			console.error("Failed to get local stream", err);
		},
	);
});

Running tests

npm test

Browser support

Firefox
Firefox
Chrome
Chrome
Safari
Edge
Safari
Safari
80+ 83+ 83+ 15+

We test PeerJS against these versions of Chrome, Edge, Firefox, and Safari with BrowserStack to ensure compatibility. It may work in other and older browsers, but we don't officially support them. Changes to browser support will be a breaking change going forward.

[!NOTE] Firefox 102+ is required for CBOR / MessagePack support.

FAQ

Q. I have a message Critical dependency: the request of a dependency is an expression in browser's console

A. The message occurs when you use PeerJS with Webpack. It is not critical! It relates to Parcel https://github.com/parcel-bundler/parcel/issues/2883 We'll resolve it when updated to Parcel V2.

Links

Documentation / API Reference

PeerServer

Discuss PeerJS on our Telegram Channel

Changelog

Contributors

This project exists thanks to all the people who contribute.

Backers

Thank you to all our backers! [Become a backer]

<img src="https://openco

readme truncated — read the full docs on github

Frequently asked questions

Is peerjs free to use?

peerjs is open source under the MIT licence. There is no licence fee and no seat count — you can self-host it or, where the project offers one, pay a vendor for a managed version instead.

What does peerjs do?

Simple peer-to-peer with WebRTC.

What is peerjs written in?

peerjs is primarily written in TypeScript. Its source is publicly available at https://github.com/peers/peerjs, and it has 13,446 GitHub stars.