npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

peer-webrtc

v1.2.1

Published

peer-webrtc simplifies the implementation of WebRTC connections by providing a straightforward API for establishing peer-to-peer communication. Ideal for developers looking to integrate real-time audio, video, and data channels into their applications wit

Downloads

24

Readme

peer-webrtc

npm version GitHub license

peer-webrtc simplifies the implementation of WebRTC connections by providing a straightforward API for establishing peer-to-peer communication. It's ideal for developers looking to integrate real-time audio, video, and data channels into their applications with minimal setup.

Features

  • Easy Setup: Simplifies the creation and management of WebRTC connections.
  • Real-Time Communication: Supports both audio/video streaming and data channels.
  • Flexible API: Offers customizable event handlers for handling various WebRTC events.

Installation

Install peer-webrtc using npm:

npm install peer-webrtc

Install peer-webrtc using yarn:

yarn add peer-webrtc

Use peer-webrtc with CDN:

<script type="module">
	import PeerWebRTC from 'https://cdn.jsdelivr.net/npm/peer-webrtc/peer-webrtc.min.js'

	const peer = new PeerWebRTC(true, mediaStream, rtcConfig);
</script>

Basic Usage

Creating a Peer Connection

To start using peer-webrtc, create a new instance of the PeerWebRTC class. You need to specify whether the peer is an initiator and optionally provide a media stream and configuration for the RTCPeerConnection.

import PeerWebRTC from 'peer-webrtc';

// Create a new PeerWebRTC instance
const peer = new PeerWebRTC(true, mediaStream, rtcConfig);
  • initiator: A boolean indicating if this peer initiates the connection.
  • mediaStream: An optional MediaStream object for audio/video streams.
  • rtcConfig: Optional configuration for the RTCPeerConnection.

Setting Up Event Handlers

You can define callbacks for various events such as receiving a signal, connection state changes, incoming data, and new streams.

peer.onSignal(async (sdp) => {
  // Send SDP to the remote peer
  sendSignalToRemotePeer(sdp);
});

peer.onConnect(() => {
  console.log('Connection established!');
});

peer.onDisconnect(() => {
  console.log('Connection lost.');
});

peer.onData((data) => {
  console.log('Received data:', data);
});

peer.onStream((stream) => {
  // Attach the stream to a video element or handle it as needed
  const videoElement = document.getElementById('remoteVideo');
  videoElement.srcObject = stream;
});

Handling Incoming Signals

When you receive a signal from the remote peer, use the signal method to process it. This will handle SDP offers and answers automatically.

async function receiveRemoteSignal(sdp) {
  await peer.signal(sdp);
}

Adding ICE Candidates

To manage ICE candidates, provide a callback to handle them and add remote candidates as they arrive.

peer.onIceCandidate((candidate) => {
  // Send the ICE candidate to the remote peer
  sendCandidateToRemotePeer(candidate);
});

async function addRemoteIceCandidate(candidate) {
  await peer.addIceCandidate(candidate);
}

Sending Data

You can send data over the established data channel using the send method.

peer.send('Hello, peer!');

Disconnecting

To gracefully close the connection, use the disconnect method.

peer.disconnect();

Advanced Usage

Custom RTCPeerConnection Configuration

You can pass a custom configuration object to the RTCPeerConnection constructor via the rtcConfig parameter. This is useful for specifying ICE servers or other advanced settings.

const rtcConfig = {
  iceServers: [
    { urls: 'stun:stun.l.google.com:19302' },
    { urls: 'turn:turn.example.com', username: 'user', credential: 'pass' }
  ]
};

const peer = new PeerWebRTC(true, mediaStream, rtcConfig);

Working with Media Streams

To send audio or video, pass a MediaStream object when creating the PeerWebRTC instance. You can obtain a media stream using the getUserMedia API.

navigator.mediaDevices.getUserMedia({ video: true, audio: true })
  .then((stream) => {
    const peer = new PeerWebRTC(true, stream);
  })
  .catch((error) => {
    console.error('Error accessing media devices.', error);
  });

API Reference

Constructor

new PeerWebRTC(initiator, stream, config);
  • initiator: Boolean - Indicates if the peer is the connection initiator.
  • stream: MediaStream - Optional media stream for audio/video.
  • config: Object - Optional RTCPeerConnection configuration.

Methods

  • onSignal(callback): Register a callback to handle signaling data.
  • onIceCandidate(callback): Register a callback to handle ICE candidates.
  • onConnect(callback): Register a callback to handle connection establishment.
  • onDisconnect(callback): Register a callback to handle connection closure.
  • onData(callback): Register a callback to handle received data.
  • onStream(callback): Register a callback to handle received media streams.
  • signal(sdp): Process an incoming SDP signal.
  • addIceCandidate(candidate): Add a remote ICE candidate.
  • send(data): Send data over the data channel.
  • disconnect(): Close the connection.

License

peer-webrtc is MIT licensed.