{"id":13632350,"url":"https://github.com/skyllo/peer-lite","last_synced_at":"2025-04-18T02:32:45.631Z","repository":{"id":37851487,"uuid":"143473944","full_name":"skyllo/peer-lite","owner":"skyllo","description":"Lightweight WebRTC browser library that supports video, audio and data channels","archived":false,"fork":false,"pushed_at":"2022-08-06T11:00:34.000Z","size":851,"stargazers_count":164,"open_issues_count":0,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-08-01T22:53:07.775Z","etag":null,"topics":["p2p","peer-to-peer","rtcpeerconnection","webrtc","webrtc-demos","webrtc-javascript-library","webrtc-libraries"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/skyllo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-08-03T21:15:49.000Z","updated_at":"2024-06-22T00:44:03.000Z","dependencies_parsed_at":"2022-07-16T23:31:01.523Z","dependency_job_id":null,"html_url":"https://github.com/skyllo/peer-lite","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skyllo%2Fpeer-lite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skyllo%2Fpeer-lite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skyllo%2Fpeer-lite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/skyllo%2Fpeer-lite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/skyllo","download_url":"https://codeload.github.com/skyllo/peer-lite/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223772183,"owners_count":17199969,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["p2p","peer-to-peer","rtcpeerconnection","webrtc","webrtc-demos","webrtc-javascript-library","webrtc-libraries"],"created_at":"2024-08-01T22:03:01.071Z","updated_at":"2024-11-09T00:31:08.919Z","avatar_url":"https://github.com/skyllo.png","language":"TypeScript","readme":"# PeerLite\n\n[![CircleCI](https://circleci.com/gh/skyllo/peer-lite.svg?style=svg\u0026circle-token=cd1df6b2a763871eb9c52ec816a40e0ba0e9beeb)](https://circleci.com/gh/skyllo/peer-lite)\n[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)\n\nLightweight WebRTC browser library that supports video, audio and data channels.\n\n# Features\n* Lightweight! 3kb (gzipped)\n* Zero dependencies\n* Ships with TypeScript definitions\n* Uses modern WebRTC APIs\n* [\"Perfect negotiation\"](https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Perfect_negotiation) pattern\n* Support for [renegotiation](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/onnegotiationneeded) of connection\n* ICE candidate batching\n\n# Installation\n```bash\nyarn add peer-lite\n```\n\n# Usage\n## Two peers connecting locally\n\n```javascript\nimport Peer from 'peer-lite';\n\nconst peer1 = new Peer();\nconst peer2 = new Peer();\n\npeer1.on('signal', async (description) =\u003e {\n  await peer2.signal(description);\n})\n\npeer2.on('signal', async (description) =\u003e {\n  await peer1.signal(description);\n})\n\npeer1.on('onicecandidates', async (candidates) =\u003e {\n  const promises = candidates.map(async candidate =\u003e peer2.addIceCandidate(candidate));\n  await Promise.all(promises);\n});\n\npeer2.on('onicecandidates', async (candidates) =\u003e {\n  const promises = candidates.map(async candidate =\u003e peer1.addIceCandidate(candidate));\n  await Promise.all(promises);\n});\n\npeer1.on('streamRemote', (stream) =\u003e {\n  document.querySelector('#video1').srcObject = stream;\n});\n\npeer2.on('streamRemote', (stream) =\u003e {\n  document.querySelector('#video2').srcObject = stream;\n});\n\n(async () =\u003e {\n  const stream = await Peer.getUserMedia();\n  peer1.addStream(stream);\n  peer2.addStream(stream);\n  peer1.start();\n})();\n```\n\n## Peer connection with fake signalling server\n\n```javascript\nimport Peer from 'peer-lite';\n\nconst peer = new Peer();\nconst fakeSocket = new Socket();\n\n// Peer events\n\npeer.on('signal', async (description) =\u003e {\n  fakeSocket.emit('signal', description);\n});\n\npeer.on('onicecandidates', async (candidates) =\u003e {\n  fakeSocket.emit('onicecandidates', candidates);\n});\n\npeer.on('streamLocal', (stream) =\u003e {\n  document.querySelector('#videoLocal').srcObject = stream;\n});\n\npeer.on('streamRemote', (stream) =\u003e {\n  document.querySelector('#videoRemote').srcObject = stream;\n});\n\n// Socket events\n\nfakeSocket.on('signal', async (description) =\u003e {\n  await peer.signal(description);\n});\n\nfakeSocket.on('onicecandidates', async (candidates) =\u003e {\n  const promises = candidates.map(async candidate =\u003e peer.addIceCandidate(candidate));\n  await Promise.all(promises);\n});\n\n(async () =\u003e {\n  const stream = await Peer.getUserMedia();\n  peer.addStream(stream);\n  peer.start();\n})();\n```\n\n# Examples\nSee more examples [here](example) with signalling server.\n\n# API\n## Constructor\n`new Peer(Options)`\n\n## Peer Options\n```typescript\ninterface PeerOptions {\n  /** Enable support for batching ICECandidates */\n  batchCandidates?: boolean;\n  /** Timeout in MS before emitting batched ICECandidates */\n  batchCandidatesTimeout?: number;\n  /** Peer id used when emitting errors */\n  id?: string;\n  /** RTCPeerConnection options */\n  config?: RTCConfiguration;\n  /** RTCOfferOptions options */\n  offerOptions?: RTCOfferOptions;\n  /** Enable support for RTCDataChannels */\n  enableDataChannels?: boolean;\n  /** Default RTCDataChannel label */\n  channelLabel?: string;\n  /** Default RTCDataChannel options */\n  channelOptions?: RTCDataChannelInit;\n  /** Function to transform offer/answer SDP */\n  sdpTransform?: (sdp: string) =\u003e string;\n}\n```\n\n## Peer API\n```typescript\ninterface Peer {\n  /** Create a peer instance */\n  constructor(options?: PeerOptions);\n  /** Initialize the peer */\n  init(): RTCPeerConnection;\n  /** Start the RTCPeerConnection signalling */\n  start({ polite }?: {\n      polite?: boolean | undefined;\n  }): void;\n  /** Process a RTCSessionDescriptionInit on peer */\n  signal(description: RTCSessionDescriptionInit): Promise\u003cvoid\u003e;\n  /** Add RTCIceCandidate to peer */\n  addIceCandidate(candidate: RTCIceCandidate): Promise\u003cvoid\u003e;\n  /** Send data to connected peer using an RTCDataChannel */\n  send(data: string | Blob | ArrayBuffer | ArrayBufferView, label?: string): boolean;\n  /** Add RTCDataChannel to peer */\n  addDataChannel(label?: string, options?: RTCDataChannelInit): void;\n  /** Get RTCDataChannel added to peer */\n  getDataChannel(label?: string): RTCDataChannel | undefined;\n  /** Close peer if active */\n  destroy(): void;\n  /** Return the ICEConnectionState of the peer */\n  status(): RTCIceConnectionState;\n  /** Return true if the peer is connected */\n  isConnected(): boolean;\n  /** Return true if the peer is closed */\n  isClosed(): boolean;\n  /** Return the RTCPeerConnection */\n  get(): RTCPeerConnection;\n  /** Return the local stream */\n  getStreamLocal(): MediaStream;\n  /** Add stream to peer */\n  addStream(stream: MediaStream, replace?: boolean): void;\n  /** Remove stream from peer */\n  removeStream(stream: MediaStream): void;\n  /** Add track to peer */\n  addTrack(track: MediaStreamTrack): void;\n  /** Remove track on peer */\n  removeTrack(track: MediaStreamTrack): void;\n  /** Remove tracks on peer */\n  removeTracks(tracks: MediaStreamTrack[]): void;\n  /** Replace track with another track on peer */\n  replaceTrack(track: MediaStreamTrack, newTrack: MediaStreamTrack): Promise\u003cvoid\u003e;\n  on\u003cE extends keyof PeerEvents\u003e(event: E, listener: PeerEvents[E]): TypedEmitter\u003cPeerEvents\u003e;\n  off\u003cE extends keyof PeerEvents\u003e(event: E, listener: PeerEvents[E]): TypedEmitter\u003cPeerEvents\u003e;\n  offAll\u003cE extends keyof PeerEvents\u003e(event?: E): TypedEmitter\u003cPeerEvents\u003e;\n}\n```\n\n## Peer Events\n```typescript\ninterface PeerEvents {\n  error: (data: { id: string; message: string; error?: Error }) =\u003e void;\n  // Connection Status\n  connecting: VoidFunction;\n  connected: VoidFunction;\n  disconnected: VoidFunction;\n  status: (status: RTCIceConnectionState) =\u003e void;\n  // Signal and RTCIceCandidates\n  signal: (description: RTCSessionDescriptionInit) =\u003e void;\n  onicecandidates: (iceCandidates: RTCIceCandidate[]) =\u003e void;\n  // MediaStreams\n  streamLocal: (stream: MediaStream) =\u003e void;\n  streamRemote: (stream: MediaStream) =\u003e void;\n  // RTCDataChannel\n  channelOpen: (data: { channel: RTCDataChannel }) =\u003e void;\n  channelClosed: (data: { channel: RTCDataChannel }) =\u003e void;\n  channelError: (data: { channel: RTCDataChannel; event: RTCErrorEvent }) =\u003e void;\n  channelData: (data: {\n    channel: RTCDataChannel;\n    source: 'incoming' | 'outgoing';\n    data: string | Blob | ArrayBuffer | ArrayBufferView;\n  }) =\u003e void;\n}\n```\n\n# Testing\nThe tests run inside a headless Chrome and Firefox with [Playwright](https://playwright.dev/)\nand [@playwright/test](https://www.npmjs.com/package/@playwright/test).\nThese run quickly and allow testing of WebRTC APIs in real browsers.\n\n**Run Tests (Chrome only)**\n```bash\nyarn test\n```\n\n**Run Tests (Chrome + Firefox)**\n```bash\nCI=true yarn test\n```\n\n# Similar Projects\n* PeerJS: https://github.com/peers/peerjs\n* Simple Peer: https://github.com/feross/simple-peer\n* SimpleWebRTC: https://github.com/andyet/SimpleWebRTC\n* More here: https://stackoverflow.com/questions/24857637/current-state-of-javascript-webrtc-libraries\n","funding_links":[],"categories":["TypeScript","p2p","Libraries"],"sub_categories":["JavaScript"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskyllo%2Fpeer-lite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fskyllo%2Fpeer-lite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fskyllo%2Fpeer-lite/lists"}