PeerLite
Lightweight WebRTC browser library that supports video, audio and data channels.
Features
- Lightweight! 7kb in size (< 3kb gzip)
- Zero dependencies
- Using modern WebRTC APIs with TypeScript Types
- "Perfect negotiation" pattern
- Support for renegotiation of connection
- ICE candidate batching
Installation
yarn add peer-lite
Usage
Example of two peers connecting to each other locally, see more examples here.
import Peer from 'peer-lite';
const peer1 = new Peer();
const peer2 = new Peer();
peer1.on('signal', async (description) => {
await peer2.signal(description);
})
peer2.on('signal', async (description) => {
await peer1.signal(description);
})
peer1.on('onicecandidates', async (candidates) => {
const promises = candidates.map(async candidate => peer2.addIceCandidate(candidate));
await Promise.all(promises);
});
peer2.on('onicecandidates', async (candidates) => {
const promises = candidates.map(async candidate => peer1.addIceCandidate(candidate));
await Promise.all(promises);
});
peer1.on('streamRemote', (stream) => {
document.querySelector('#video1').srcObject = stream;
});
peer2.on('streamRemote', (stream) => {
document.querySelector('#video2').srcObject = stream;
});
(async () => {
const stream = await Peer.getUserMedia();
peer1.addStream(stream);
peer2.addStream(stream);
peer1.start();
})();
API
Constructor
new Peer(Options)
Options
Typescript Options Definition
export interface PeerOptions {
batchCandidates?: boolean;
batchCandidatesTimeout?: number;
enableDataChannels?: boolean;
name?: string;
config?: RTCConfiguration;
constraints?: MediaStreamConstraints;
offerOptions?: RTCOfferOptions;
answerOptions?: RTCAnswerOptions;
channelName?: string;
channelOptions?: RTCDataChannelInit;
sdpTransform?: (sdp: string) => string;
}
Testing
The tests run inside a headless Chrome with Playwright
using @playwright/test with Jest.
These run quickly and allow testing of real WebRTC APIs in a real browser.
yarn test
Similar Projects