explaingit

peers/peerjs

13,327TypeScriptAudience · developerComplexity · 2/5Setup · moderate

TLDR

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.

Mindmap

mindmap
  root((PeerJS))
    Connection Types
      Data connections
      Media connections
    How It Works
      WebRTC core
      PeerServer handshake
      Direct P2P after
    Use Cases
      Video calls
      File sharing
      Multiplayer games
    Setup
      npm install
      PeerServer needed
      Modern browsers only
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Build browser-to-browser video calls without paying for media relay servers, using each browser's built-in WebRTC support.

USE CASE 2

Create a multiplayer game or collaborative tool where game state flows directly between players' browsers with low latency.

USE CASE 3

Let users share files directly between two browsers without uploading them to a server first.

Tech stack

TypeScriptJavaScriptWebRTC

Getting it running

Difficulty · moderate Time to first run · 30min

Requires a PeerServer instance (self-hosted or cloud) to broker the initial connection between peers before direct communication begins.

In plain English

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.

Copy-paste prompts

Prompt 1
Using PeerJS, write the JavaScript code for a simple video call between two browser tabs where each user enters the other's peer ID to connect.
Prompt 2
Show me how to send a JSON object from one PeerJS client to another using a data connection and handle it on the receiving side.
Prompt 3
How do I set up a self-hosted PeerServer on Node.js and configure PeerJS clients to use it instead of the default cloud service?
Prompt 4
Write PeerJS error handling that detects when a peer disconnects and automatically attempts to reconnect with exponential backoff.
Open on GitHub → Explain another repo

← peers on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.