Skip to content

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 with minimal setup.

License

Notifications You must be signed in to change notification settings

Abhay-Vachhani/peer-webrtc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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 with minimal setup.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published