https://github.com/skyllo/peer-lite Skip to content Sign up * Product + Features + Mobile + Actions + Codespaces + Packages + Security + Code review + Issues + Integrations + GitHub Sponsors + Customer stories * Team * Enterprise * Explore + Explore GitHub + Learn and contribute + Topics + Collections + Trending + Skills + GitHub Sponsors + Open source guides + Connect with others + The ReadME Project + Events + Community forum + GitHub Education + GitHub Stars program * Marketplace * Pricing + Plans + Compare plans + Contact Sales + Education [ ] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} skyllo / peer-lite Public * Notifications * Fork 1 * Star 20 Lightweight WebRTC browser library that supports video, audio and data channels License MIT license 20 stars 1 fork Star Notifications * Code * Issues 0 * Pull requests 0 * Actions * Projects 0 * Wiki * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Wiki * Security * Insights skyllo/peer-lite This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. master Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags 1 branch 10 tags Code Latest commit @skyllo skyllo test: cleanup test for starting peer before listeners added ... 57e9e00 Jun 6, 2022 test: cleanup test for starting peer before listeners added 57e9e00 Git stats * 127 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .circleci test: add firefox as a browser for tests May 23, 2022 build chore: make typescript only emit declarations Jun 2, 2022 example chore: rename name to id in peer options May 31, 2022 src chore: cleanup code comments Jun 5, 2022 test test: cleanup test for starting peer before listeners added Jun 6, 2022 .editorconfig chore: update dependencies and add prettier Nov 12, 2021 .eslintignore feat: add swc, remove babel and jest Mar 20, 2022 .eslintrc.js feat: add offAll function to emitter Jun 2, 2022 .gitignore feat: add examples with signal server and parcel dev server May 26, 2019 .prettierrc chore: update dependencies and add prettier Nov 12, 2021 .yarnrc feat: initial commit May 23, 2019 LICENSE docs: update LICENSE May 23, 2022 README.md docs: update README.md Jun 2, 2022 package.json v2.0.1 Jun 5, 2022 playwright.config.ts test: only run firefox tests in CI May 23, 2022 tsconfig.json feat: add offAll function to emitter Jun 2, 2022 yarn.lock chore: update depedencies Jun 5, 2022 View code [ ] PeerLite Features Installation Usage Two peers connecting locally Peer connection with fake signalling server Examples API Constructor Peer Options Peer API Peer Events Testing Similar Projects README.md PeerLite CircleCI Lightweight WebRTC browser library that supports video, audio and data channels. Features * Lightweight! 3kb (gzipped) * Zero dependencies * Ships with TypeScript definitions * Uses modern WebRTC APIs * "Perfect negotiation" pattern * Support for renegotiation of connection * ICE candidate batching Installation yarn add peer-lite Usage Two peers connecting locally 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(); })(); Peer connection with fake signalling server import Peer from 'peer-lite'; const peer = new Peer(); const fakeSocket = new Socket(); // Peer events peer.on('signal', async (description) => { fakeSocket.emit('signal', description); }); peer.on('onicecandidates', async (candidates) => { fakeSocket.emit('onicecandidates', candidates); }); peer.on('streamLocal', (stream) => { document.querySelector('#videoLocal').srcObject = stream; }); peer.on('streamRemote', (stream) => { document.querySelector('#videoRemote').srcObject = stream; }); // Socket events fakeSocket.on('signal', async (description) => { await peer.signal(description); }); fakeSocket.on('onicecandidates', async (candidates) => { const promises = candidates.map(async candidate => peer.addIceCandidate(candidate)); await Promise.all(promises); }); (async () => { const stream = await Peer.getUserMedia(); peer.addStream(stream); peer.start(); })(); Examples See more examples here with signalling server. API Constructor new Peer(Options) Peer Options interface PeerOptions { /** Enable support for batching ICECandidates */ batchCandidates?: boolean; /** Timeout in MS before emitting batched ICECandidates */ batchCandidatesTimeout?: number; /** Peer id used when emitting errors */ id?: string; /** RTCPeerConnection options */ config?: RTCConfiguration; /** RTCOfferOptions options */ offerOptions?: RTCOfferOptions; /** Enable support for RTCDataChannels */ enableDataChannels?: boolean; /** Default RTCDataChannel label */ channelLabel?: string; /** Default RTCDataChannel options */ channelOptions?: RTCDataChannelInit; /** Function to transform offer/answer SDP */ sdpTransform?: (sdp: string) => string; } Peer API interface Peer { /** Create a peer instance */ constructor(options?: PeerOptions); /** Initialize the peer */ init(): RTCPeerConnection; /** Start the RTCPeerConnection signalling */ start({ polite }?: { polite?: boolean | undefined; }): void; /** Process a RTCSessionDescriptionInit on peer */ signal(description: RTCSessionDescriptionInit): Promise; /** Add RTCIceCandidate to peer */ addIceCandidate(candidate: RTCIceCandidate): Promise; /** Send data to connected peer using an RTCDataChannel */ send(data: string | Blob | ArrayBuffer | ArrayBufferView, label?: string): boolean; /** Add RTCDataChannel to peer */ addDataChannel(label?: string, options?: RTCDataChannelInit): void; /** Get RTCDataChannel added to peer */ getDataChannel(label?: string): RTCDataChannel | undefined; /** Close peer if active */ destroy(): void; /** Return the ICEConnectionState of the peer */ status(): RTCIceConnectionState; /** Return true if the peer is connected */ isConnected(): boolean; /** Return true if the peer is closed */ isClosed(): boolean; /** Return the RTCPeerConnection */ get(): RTCPeerConnection; /** Return the local stream */ getStreamLocal(): MediaStream; /** Add stream to peer */ addStream(stream: MediaStream, replace?: boolean): void; /** Remove stream from peer */ removeStream(stream: MediaStream): void; /** Add track to peer */ addTrack(track: MediaStreamTrack): void; /** Remove track on peer */ removeTrack(track: MediaStreamTrack): void; /** Remove tracks on peer */ removeTracks(tracks: MediaStreamTrack[]): void; /** Replace track with another track on peer */ replaceTrack(track: MediaStreamTrack, newTrack: MediaStreamTrack): Promise; on(event: E, listener: PeerEvents[E]): TypedEmitter; off(event: E, listener: PeerEvents[E]): TypedEmitter; offAll(event?: E): TypedEmitter; } Peer Events interface PeerEvents { error: (data: { id: string; message: string; error?: Error }) => void; // Connection Status connecting: VoidFunction; connected: VoidFunction; disconnected: VoidFunction; status: (status: RTCIceConnectionState) => void; // Signal and RTCIceCandidates signal: (description: RTCSessionDescriptionInit) => void; onicecandidates: (iceCandidates: RTCIceCandidate[]) => void; // MediaStreams streamLocal: (stream: MediaStream) => void; streamRemote: (stream: MediaStream) => void; // RTCDataChannel channelOpen: (data: { channel: RTCDataChannel }) => void; channelClosed: (data: { channel: RTCDataChannel }) => void; channelError: (data: { channel: RTCDataChannel; event: RTCErrorEvent }) => void; channelData: (data: { channel: RTCDataChannel; source: 'incoming' | 'outgoing'; data: string | Blob | ArrayBuffer | ArrayBufferView; }) => void; } Testing The tests run inside a headless Chrome and Firefox with Playwright and @playwright/test. These run quickly and allow testing of WebRTC APIs in real browsers. Run Tests (Chrome only) yarn test Run Tests (Chrome + Firefox) CI=true yarn test Similar Projects * PeerJS: https://github.com/peers/peerjs * Simple Peer: https://github.com/feross/simple-peer * SimpleWebRTC: https://github.com/andyet/SimpleWebRTC * More here: https://stackoverflow.com/questions/24857637/ current-state-of-javascript-webrtc-libraries About Lightweight WebRTC browser library that supports video, audio and data channels Topics webrtc peer-to-peer p2p webrtc-javascript-library webrtc-demos webrtc-libraries rtcpeerconnection Resources Readme License MIT license Stars 20 stars Watchers 2 watching Forks 1 fork Releases 10 v2.0.1 Latest Jun 6, 2022 + 9 releases Packages 0 No packages published Used by 4 * @Dviros * @mattsoulanille * @pramasoul * @vivianeasley Contributors 2 * @skyllo skyllo Nick * @dependabot[bot] dependabot[bot] Languages * TypeScript 93.0% * JavaScript 4.7% * HTML 2.3% * (c) 2022 GitHub, Inc. * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.