Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
P

peerjs

> 编程语言
Open source

Simple peer-to-peer with WebRTC.

13.4K stars0 likes0 views
WebsiteGitHub

About

Simple peer-to-peer with WebRTC.

PeerJS: Simple peer-to-peer with WebRTC

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

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

Create a Peer

javascript
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

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

Receive

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

Media calls

Call

javascript
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

javascript
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

bash
npm test

Browser support


Firefox

Chrome

Edge

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]

<

Issues· 203 open

View all issuesOpen on GitHub
  • #1370

    `Peer`'s constructor overloads can't express "no id, but here are options"

    Updated Sep 21, 2026
  • #1345

    Peer.disconnected isn't reliable + Peer.connect doesn't wait for it

    bugunconfirmedUpdated Jun 29, 2026
  • #1367

    Missing Privacy Policy for the public server node

    bugunconfirmedUpdated Jun 9, 2026
  • #1355

    peer.on("connection") below the connection.on("data") invalid does not receive a message

    bugunconfirmedUpdated Mar 23, 2026
  • #1014

    Dependency Dashboard

    dependenciesclientUpdated Feb 27, 2026
  • #1352

    bug in docs on peerjs.com

    bugunconfirmedUpdated Feb 24, 2026
  • #769

    Handling broken connections

    clientUpdated Feb 6, 2026
  • #1350

    Extreme WebSocket connection delay (6+ minutes) on 0.peerjs.com

    bugunconfirmedUpdated Oct 25, 2025
  • #924

    Error expected on DataConnection when not establishing a connection

    help wantedfeatureclientUpdated Oct 18, 2025
  • #1312

    Calling `close` on an incoming UNANSWERED call does not fire the outgoing call's `close` event

    bugunconfirmedUpdated Oct 3, 2025

> Tags

TypeScriptbrowserdataconnectionjavascriptp2p

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 2, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言