@rtco/peer
TypeScript icon, indicating that this package has built-in type declarations

0.2.19 • Public • Published

@rtco/peer

Artico peer library. Please refer to the documentation for more information.

Installation

npm install @rtco/peer

Usage

The following example show how to connect two peers and share audio/video or any data between them:

import Peer from "@rtco/peer";

const p1 = new Peer({ initiator: true });
const p2 = new Peer();

p1.on("signal", (data) => {
  // signal p2 somehow
  p2.signal(data);
});

p2.on("signal", (data) => {
  // signal p1 somehow
  p1.signal(data);
});

p1.on("connect", () => {
  // data channel is connected and ready to be used
  p1.send("Hey Peer 2, this is Peer 1!");
});

p2.on("data", (data) => {
  console.log("Received a message from Peer 1:", data);
});

p2.on("stream", (stream, metadata) => {
  // when adding streams to a connection, we can provide any object as metadata
  console.log("Received new stream from Peer 1:", metadata);
});

// ...

navigator.mediaDevices
  .getUserMedia({
    video: true,
    audio: true,
  })
  .then((stream) => {
    // send stream to Peer 2 with metadata indicating type of stream
    p1.addStream(stream, {
      type: "camera",
    });
  })
  .catch(console.error);

Dependencies (1)

Dev Dependencies (3)

Package Sidebar

Install

npm i @rtco/peer

Weekly Downloads

156

Version

0.2.19

License

MIT

Unpacked Size

11.6 kB

Total Files

4

Last publish

Collaborators

  • matallui