Web Rtc Connector

Projects technologies

WebRtc Connector Client

Waring: This project hasn't been audited by security researchers, therefore please don't use it in Production environment

Installing npm package:

npm install @amir4rab/web-rtc-connector-client@latest

Instantiating an instance:

make sure that your signaling server is online then continue with WebRtc Connector Client

const webRtc = new WebRtc({ serverUrl: 'your signaling server url' });

Getting WebRtc instance Details:

your id is exposed to the server but secret isn't, and it has been generated automatically by the client. you should send these two items to the other peer.

webRtc.on('onConnection', ({ id, secret }) => { console.log(id, secret); });

WebRtc Data-channel:

Making a data-channel

webRtc.dataConnection({ id: 'other peer ID', secret: 'other peer Secret' });

Events

webRtc.on('onDataChannel', (dataChannel) => { // "onDataChannel" will be called after dataChannel initialization // you can update UI of your web-app // eg: connection state: "Connected" }); webRtc.on('onMessage', (message) => { // "onMessage" will be called, when receiving a message from WebRtc Peer });

Sending message

// this function should only be called after Data-channel has been initialization webRtc.sendMessage('your message');

WebRtc Media Connection:

Answering call

For Media connection, first pair need to be ready to answer calls

const answerCall = async (_) => { mediaStream = stream = await navigator.mediaDevices.getUserMedia( 'constraints' ); await webRtc.answerMediaConnection(mediaStream); };

Making a call

const makeCall = async (_) => { mediaStream = stream = await navigator.mediaDevices.getUserMedia( 'constraints' ); await webRtc.makeMediaConnection(mediaStream, { id: 'other peer ID', secret: 'other peer Secret' }); };

Events

webRtc.on('onStream', (remoteStream) => { // "onStream" will be Called after receiving Remote stream // you can update UI of your web-app // eg: peerVideo.srcObject = remoteStream; });

Functions

to update WebRtc tracks you can pass a new media stream to updateMedia function.

const updateMedia = async (_) => { await webRtc.updateMedia(mediaStream); };

Functions:

Closing Connection

await webRtc.close();

Events:

Checking Call Security

webRtc.on('descriptionsCompleted', async ({ hashObj }) => { // console.log( hashObj.hash ) // to users can check if their hashes are equal });

Call-end event

webRtc.on('onClose', (_) => { // "onClose" will be called when connection has been closed! });

Error event

webRtc.on('onError', (_) => { // "onError" will be called when connection has been failed! });

WebRtc Connector Server

Waring: This project hasn't been audited by security researchers, therefore please don't use it in Production environment

WebRtcConnector Signaling server is, a socket.io server, modify it for your use case.

Setup

cloning

git clone https://github.com/amir4rab/webRtcConnector && cd ./server

installing dependencies

npm install

making environment variables

make .env file and add these two variables to it

PORT: 'Port for your signaling server' ORIGIN: 'Origin of your webapp'

Learn more