peerjs README

The project's README from github.com/peers/peerjs. As one markdown file: README.md.

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.

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]

Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]

License

PeerJS is licensed under the MIT License.

peerjs (peers/peerjs)

Searching Database…

13.5K ·GitHub
这是什么?PeerJS 是一个运行在浏览器端的 JavaScript/TypeScript 库,它把 WebRTC 封装成一套简单、可配置的点对点(P2P)API,同时支持数据通道和音视频流。
PeerJS 是打包进我的网页应用里的库,还是需要我自己部署的服务?它是一个客户端库,通过 npm 或 yarn 引入到网页应用中——点对点的工作都是在浏览器里通过 WebRTC API 完成的。
怎么把它加到项目里?先用 npm('npm install peerjs')或 yarn('yarn add peerjs')安装,然后在代码里创建一个 Peer,就可以开始建立连接了。
我需要自己跑一个服务器,让各个节点互相找到对方吗?需要——建立连接要经过 PeerServer,这是一个单独的配套组件,README 里的在线示例就运行了自己的一个实例。连接建立之后,数据和音视频就在各个节点之间直接传输。
它到底能在哪些浏览器里运行?Firefox 80+、Chrome 83+、Edge 83+ 和 Safari 15+ 是官方支持并经过 BrowserStack 测试的版本。其他或更旧的浏览器也许能用,但不属于官方支持范围。
它能做音频和视频通话吗,还是只能传数据?都可以。它既支持用来传消息的数据通道,也支持媒体流,针对媒体连接还有专门的 call 和 answer 步骤。
一段最简单的连接代码是什么样的?先创建一个 Peer;如果是数据连接,接着就是 connect 和接收,如果是媒体通话,就是 call 和 answer——README 正是围绕这几个步骤组织的。
它支持 CBOR 或 MessagePack 这类结构化数据吗?支持——它内置了 CBOR 和 MessagePack 支持,不过这部分需要 Firefox 102 或更新的版本。
用 Webpack 打包时有什么坑吗?控制台可能会出现 'Critical dependency: the request of a dependency is an expression' 的提示。根据 FAQ,这并不要紧,它和 Parcel 有关,等项目升级到 Parcel V2 后就会解决。
Creator
peers
Size
10.8 MB
Stars
13.5K
Age
13y
Commits · 90 Days