Rooms
Join a room by name. Every other peer in it becomes a participant with its own RTCPeerConnection, and
leaving closes them all.
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-dataconst peer = new PeerData(signaling);const room = peer.connect('lobby');
// Every other peer in the roomroom.on('participant', (participant) => { participant.on('message', (data) => { console.log(participant.id, 'says', data); }); participant.send('hello');});const stream = await navigator.mediaDevices .getUserMedia({ video: true, audio: true });
// Everyone in the room receives your tracksconst room = peer.connect('call', stream);
room.on('participant', (participant) => { participant.on('track', ({ streams: [theirs] }) => { if (theirs) video.srcObject = theirs; });});// Between tabs, with no servernew PeerData(new BroadcastChannelSignaling());
// Through your socket.io servernew PeerData(new SocketChannel(io(url)));
// Or anything that sends and subscribesnew PeerData({ send, subscribe });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.
Connecting…
Connecting…
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.
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();});const peerData = new PeerData( new SocketChannel(io('http://localhost:8080')), { configuration: servers },);
const room = peerData.connect('room');room.on('participant', (participant) => { participant.addStream(stream);});Install the package, join a room, and send your first message. The guide starts with no server and ends with one.