https://github.com/abhay-vachhani/peer-webrtc
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.
https://github.com/abhay-vachhani/peer-webrtc
audio communication data-channels peer peer-to-peer peer-webrtc real-time rtc simple-peer video webrtc
Last synced: 4 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/abhay-vachhani/peer-webrtc
- Owner: Abhay-Vachhani
- License: mit
- Created: 2024-06-12T15:07:28.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-06-19T10:55:50.000Z (almost 2 years ago)
- Last Synced: 2025-08-23T18:24:30.112Z (10 months ago)
- Topics: audio, communication, data-channels, peer, peer-to-peer, peer-webrtc, real-time, rtc, simple-peer, video, webrtc
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/peer-webrtc
- Size: 7.81 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: License.md
Awesome Lists containing this project
README
# peer-webrtc
[](https://badge.fury.io/js/peer-webrtc)
[](https://github.com/Abhay-Vachhani/peer-webrtc/blob/master/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:
```bash
npm install peer-webrtc
```
Install `peer-webrtc` using yarn:
```bash
yarn add peer-webrtc
```
Use `peer-webrtc` with CDN:
```html
import PeerWebRTC from 'https://cdn.jsdelivr.net/npm/peer-webrtc/peer-webrtc.min.js'
const peer = new PeerWebRTC(true, mediaStream, rtcConfig);
```
## 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.
```javascript
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.
```javascript
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.
```javascript
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.
```javascript
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.
```javascript
peer.send('Hello, peer!');
```
### Disconnecting
To gracefully close the connection, use the `disconnect` method.
```javascript
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.
```javascript
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.
```javascript
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
```javascript
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](https://github.com/Abhay-Vachhani/peer-webrtc/blob/master/License).