Easy to use wrapper for peerJS WebRTC library
(Currently focussed more on messaging. but video stream also works, not fully tested)
Live Demo
- Build a functional webRTC in less than 10 lines of code.
- Its a wrapper for peerJS library.
- easily establish multi-node connections.
- inbuilt Auto sync all nodes to create a mesh network.
Add both files in your HTML
<script src="https://unpkg.com/[email protected]/dist/peerjs.min.js"></script>
<script src="simpleWebRTC.js"></script>
Javascript
//for custom id pass id:string instead of null
const webRTC = new simpleWebRTC(null ,{
onInit : myID => { }, // on connection to peerJS
onMessage : ({data, id}) => { }, // on recieving data from any node
remoteMedia : list => { }, // on recieving a remote media (Audio/Video)
// list = [ {id , stream},... ]
onSync : id => { ... }, // on adding a new node in network
logger : data => console.log(data), // logs everything
video : true, // true to allow video stream
audio : true // true to allow audio stream
});
//To connect to the network by passing any remote node id.
//Auto sync will take care of connecting in all nodes.
webRTC.joinID(id);
//To send message to all connected nodes
webRTC.sendData(message);
//To send message to one remote node
webRTC.sendData(message, id);
//To send message to one or more remote node
webRTC.sendData(message, [id1, id2, ..]);
//To begin Media stream to a specific remote node
webRTC.beginCall(id);
//my id
webRTC.id : string
//connected remote id list
webRTC.connectedID : string array
//remote stream list
webRTC.remoteStream : object array