Skip to content
v4See what changed

Browser to browser, directly

Join a room, and every other peer in it is a participant you can send data to and share media with, peer to peer. peer-data handles the WebRTC negotiation; you choose how peers find each other.

npm install peer-data
main.ts
const peer = new PeerData(signaling);
const room = peer.connect('lobby');
// Every other peer in the room
room.on('participant', (participant) => {
participant.on('message', (data) => {
console.log(participant.id, 'says', data);
});
participant.send('hello');
});
  • 0runtime dependencies
  • 3browser engines its tests run real connections in
  • 2methods to write your own signaling
  • v3message format, so v3 signaling servers route v4 peers

Features

Rooms

Join a room by name. Every other peer in it becomes a participant with its own RTCPeerConnection, and leaving closes them all.

Data channels

Send text or binary data to one participant or the whole room. What you send before the channel opens waits, and goes out in order.

Media at any time

Share a stream when you join or later. Connections renegotiate by themselves, and offers that cross are resolved with perfect negotiation.

Your signaling

socket.io with the client that matches your server, BroadcastChannel between tabs with no server, or your own in two methods.

Typed

Written in strict TypeScript: event names and what they carry are typed, and so is every signaling message.

Nothing else to install

An ES module with no dependencies. Its tests run real connections in Chromium, Firefox and WebKit.

Try it

These two peers found each other through BroadcastChannel and chat over a WebRTC data channel, with no server. Open this page in another tab and it joins the same chat.

Peer 1

Connecting…

    Peer 2

    Connecting…

      What went through signaling

      Everything this page's peers sent each other to connect. After that, chat messages go straight from one peer to the other.

        Upgrading

        Rooms, participants and their events are the same. The EventDispatcher is gone: PeerData takes the signaling, SocketChannel takes a socket you create, and connections negotiate by themselves.

        v3
        const dispatcher = new EventDispatcher();
        const peerData = new PeerData(dispatcher, servers);
        new SocketChannel(dispatcher, 'http://localhost:8080');
        const room = peerData.connect('room');
        room.on('participant', (participant) => {
        participant.addStream(stream);
        participant.renegotiate();
        });
        v4
        const peerData = new PeerData(
        new SocketChannel(io('http://localhost:8080')),
        { configuration: servers },
        );
        const room = peerData.connect('room');
        room.on('participant', (participant) => {
        participant.addStream(stream);
        });

        Connect two tabs in five minutes

        Install the package, join a room, and send your first message. The guide starts with no server and ends with one.