webConnect.js

📶 WebRTC peer-to-peer connection without signaling server for static web

NPM npm version

Demo : https://nuzulul.github.io/webConnect.js/demo/

Why?

I got tired of building and maintenance signaling server for my WebRTC projects

How it works?

A direct browser to browser connection for static client side web pages is normally impossible. Previously, the solution available was using WebRTC transport, which still required to setup a signaling server as a middleman. This library works by leveraging already established P2P networks, such as IPFS Network or Torrent Network for signaling, eliminating the need for backend servers forever. Now static web pages can talk to each other, even hosts on static hosting such as github pages, cloudflare pages, gitlab pages, netlify or localhost.

Features

Installation

CDN

<script src="https://cdn.jsdelivr.net/npm/webconnect@0.0.3/dist/umd/webconnect.js"></script>
<script type="module">
	import webconnect from 'https://cdn.jsdelivr.net/npm/webconnect@0.0.3/dist/esm/webconnect.js'
</script>

Browserify

npm install webconnect

//Common JS 
const webconnect = require('webconnect')

//ES Module
import webconnect from 'webconnect'

Get Started

Initialization

const connect = webconnect({})

Listen

Listen on new connect

connect.onConnect((attribute) => console.log(`${attribute.connectId} connected`))

Listen on disconnect event

connect.onDisconnect((attribute) => console.log(`${attribute.connectId} disconnected`))

Listen on receiving data

connect.onReceive((data,attribute) => console.log(`${data} from ${attribute.connectId}`))

Listen on sending progress

connect.onSendProgress((attribute) => console.log(`Sending progress : ${attribute.percent} to ${attribute.connectId}`))

Listen on receiving progress

connect.onReceiveProgress((attribute) => console.log(`Receiving progress : ${attribute.percent} from ${attribute.connectId}`))

Listen on incoming streaming

connect.onStreaming((stream,attribute) => Elements[attribute.connectId].video.srcObject = stream )

Action

Get My Connection Id

connect.getMyId((attribute) => console.log(`${attribute.connectId}`))

Send Data

const attribute = {connectId}
connect.Send(data,attribute)

Send Binary

const attribute = {connectId,metadata:{name: 'Report', type: 'application/pdf'}}
connect.Send(buffer,attribute)

Open Streaming

const attribute = {connectId,metadata:{name:'Meeting'}}
connect.openStreaming(stream,attribute)

Close Streaming

const attribute = {connectId}
connect.closeStreaming(stream,attribute)

Ping Connection

const attribute = {connectId}
console.log(`${await connect.Ping(attribute)} ms`)

Disconnect

connect.Disconnect()

Get All Connection Id

connect.getConnection((attribute) => console.log(`${attribute.connection}`))

Example

<script type="module">
	import webconnect from 'https://cdn.jsdelivr.net/npm/webconnect@0.0.3/dist/esm/webconnect.js'
	
	const connect = webconnect({})
	connect.onConnect(async(attribute)=>{
		console.log("Connect",attribute)
		connect.Send("hello",{connectId:attribute.connectId})
		console.log(await connect.Ping({connectId:attribute.connectId}))
		connect.getConnection((attribute)=>{
			console.log("Connection",attribute)
		})
	})
	connect.onDisconnect((attribute)=>{
		console.log("Disconnect",attribute)
	})
	
	connect.onReceive((data,attribute) =>{
		console.log(data,attribute)
	})
</script>

API

Connect to a channel

webconnect({appName,channelName,connectPassword,iceConfiguration})

options :

Listen to every new connection

onConnect((attribute)=>{})

Listen to every disconnection

onDisconnect((attribute)=>{})

Send data to connection

Send(data,attribute)

Send binary data to connection

Send(ArrayBuffer,attribute)

Listen to sending progress for binary data

onSendProgress((attribute) => {})

Listen to receiving data

onReceive((data,attribute) => {})

Listen to receiving progress for binary data

onReceiveProgress((attribute) => {})

Open streaming connection

openStreaming(stream,attribute)

Listen to incoming streaming connection

onStreaming((stream,attribute) => {})

Close streaming connection

closeStreaming(stream,attribute)

Get self connection identity

getMyId((attribute) => {})
getConnection((attribute) => {})

Get latency of connection which return a promise that resolve to milliseconds

Ping(attribute)

Disconnect from channel

Disconnect()

License

MIT