Socket
Book a DemoInstallSign in
Socket

simple-peer

Package Overview
Dependencies
Maintainers
6
Versions
121
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-peer

Simple one-to-one WebRTC video/voice and data channels

9.11.1
latest
Source
npmnpm
Version published
Maintainers
6
Created

What is simple-peer?

The simple-peer package is a lightweight library for WebRTC (Web Real-Time Communication) that allows developers to easily create peer-to-peer connections in the browser. It abstracts the complexities of WebRTC and provides a simple API for establishing video, audio, and data connections between peers.

What are simple-peer's main functionalities?

Creating a Peer Connection

This code demonstrates how to create a peer-to-peer connection using simple-peer. Two peers are created, one as the initiator and the other as a responder. They exchange signaling data to establish a connection.

const Peer = require('simple-peer');
const peer1 = new Peer({ initiator: true });
const peer2 = new Peer();

peer1.on('signal', data => {
  peer2.signal(data);
});

peer2.on('signal', data => {
  peer1.signal(data);
});

peer1.on('connect', () => {
  console.log('Peer1 connected to Peer2');
});

peer2.on('connect', () => {
  console.log('Peer2 connected to Peer1');
});

Sending Data Between Peers

Once the peers are connected, data can be sent between them. In this example, Peer1 sends a message to Peer2, which logs the received message.

peer1.on('connect', () => {
  peer1.send('Hello Peer2!');
});

peer2.on('data', data => {
  console.log('Received message from Peer1:', data.toString());
});

Streaming Video Between Peers

This example shows how to stream video between peers. Peer1 captures video and audio from the user's media devices and adds it to the peer connection. Peer2 receives the stream and plays it in a video element.

navigator.mediaDevices.getUserMedia({ video: true, audio: true }).then(stream => {
  peer1.addStream(stream);
});

peer2.on('stream', stream => {
  const video = document.createElement('video');
  document.body.appendChild(video);
  video.srcObject = stream;
  video.play();
});

Other packages similar to simple-peer

Keywords

data

FAQs

Package last updated on 17 Feb 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.