peerjs
Simple peer-to-peer with WebRTC.
A JavaScript library that connects two browsers directly to each other for real-time data, audio, or video transfer, no server relay needed once the connection is established, using WebRTC built into modern browsers.
PeerJS is a JavaScript library that makes it straightforward to connect two browsers directly to each other without routing all data through a server. This technique is called peer-to-peer communication, and it is built on top of WebRTC, a technology built into modern browsers that allows direct audio, video, and data transfer between devices.
The library supports two kinds of connections. Data connections let two browsers send arbitrary data back and forth: text, files, objects, or any other structured information. Media connections let two browsers share live audio and video streams, which is what you would use to build a video call or voice chat feature. Both work through the same simple API: you create a Peer object with an ID, connect to another peer by its ID, and then send or receive data through the resulting connection object.
Behind the scenes, PeerJS uses a small server called PeerServer to handle the initial handshake between peers, because two browsers cannot find each other without some way to exchange connection details. Once connected, the actual data flows directly between browsers without passing through the server, which keeps latency low and avoids server bandwidth costs for the data itself. PeerServer is a separate open-source project that you can self-host or use as a cloud service.
The library is written in TypeScript, installable via npm or yarn, and works in Firefox 80+, Chrome 83+, Edge 83+, and Safari 15+. The README includes short code examples for both data and media connections, and the project has a live demo on Glitch showing a video call application built with PeerJS.
PeerJS is open source and funded through Open Collective. It is maintained by the peers organization on GitHub and has an active Discord community for questions and roadmap discussions.
Where it fits
- Build browser-to-browser video calls without paying for media relay servers, using each browser's built-in WebRTC support.
- Create a multiplayer game or collaborative tool where game state flows directly between players' browsers with low latency.
- Let users share files directly between two browsers without uploading them to a server first.