Reusable Angular/Ionic library for WebRTC peer-to-peer video calling with Socket.IO signaling
npm install video-call-libbash
npm install video-call-lib socket.io-client
`
Quick Start
$3
`typescript
import { NgModule } from '@angular/core';
import { VideoCallLibModule } from 'video-call-lib';
@NgModule({
imports: [VideoCallLibModule]
})
export class AppModule { }
`
$3
`typescript
import { setSignalingUrl } from 'video-call-lib';
// In your app initialization (app.component.ts or main.ts)
setSignalingUrl('http://localhost:3000');
// For production
// setSignalingUrl('https://your-signaling-server.com');
`
$3
`typescript
import { Component, ViewChild, ElementRef } from '@angular/core';
import { WebRTCService } from 'video-call-lib';
@Component({
selector: 'app-call',
template:
})
export class CallComponent {
@ViewChild('localVideo') localVideo!: ElementRef;
@ViewChild('remoteVideo') remoteVideo!: ElementRef;
roomId = 'test-room';
constructor(private webrtc: WebRTCService) {}
async startCall() {
await this.webrtc.init(
this.roomId,
this.localVideo.nativeElement,
this.remoteVideo.nativeElement
);
}
endCall() {
this.webrtc.leave();
}
}
`
API Reference
$3
`typescript
// Initialize a call
async init(room: string, localVideoElement: HTMLVideoElement, remoteVideoElement: HTMLVideoElement): Promise
// Leave the call
leave(): void
// Switch between front and rear camera
async switchCamera(): Promise
// Toggle audio track
toggleAudio(enabled: boolean): void
// Toggle video track
toggleVideo(enabled: boolean): void
// Pause all tracks
pause(): void
// Resume all tracks
resume(): void
`
$3
`typescript
// Set the current room ID
setRoom(id: string): void
// Get room ID as observable
getRoom(): Observable
`
$3
Protect routes that require authentication:
`typescript
const routes: Routes = [
{
path: 'call',
component: CallComponent,
canActivate: [AuthGuard]
}
];
`
Signaling Server
The library requires a WebRTC signaling server for connection negotiation.
$3
`bash
Start the signaling server
node server.js
Server runs on http://localhost:3000
`
$3
Configure your signaling server URL based on your environment:
`typescript
// Development
setSignalingUrl('http://localhost:3000');
// Android Emulator
setSignalingUrl('http://10.0.2.2:3000');
// Production
setSignalingUrl('https://your-signaling-server.com');
`
$3
`typescript
import { environment } from './environments/environment';
import { setSignalingUrl } from 'video-call-lib';
@NgModule({})
export class AppModule {
constructor() {
setSignalingUrl(environment.signalingUrl);
}
}
``