19
loading...
This website collects cookies to deliver better user experience
Just for a quick note I'm not going in depth how WebRTC work. If you're interested you can read WebRTC For The Curious book.
You can understand WebRTC Lingo by clicking here
Connecting:- Now we know enough about each other. But the problem arises that we only know each other at a higher level in a networking term at the application layer, we should know each other's limitations and find a better way to communicate at the network layer that's why we share ICE candidates.Why we need ICE more here.
ICE candidate in layman's terms would be like hey this is my phone number, this is my email, my house address these all can be an ICE candidate. In simple terms, ICE candidate is just "These are some paths by which you can access me" it can be HostName(local Ip Addr), Server reflexive(NAT mapping), Relay(Proxy server/TURN server), etc.
Because of ICE candidates, we can be stay connected on the move:- New ICE candidate as you move(Wifi/LTE). Switch to between better connection when present(Auto-negotiation by ICE Restart)
Securing:-DTLS(TLS over UDP)+SRTP, Encryption over RTP. That mean now you have End to End Encryption(E2EE). No more man-in-middle.
Communication:- Now lets communicate, MediaCommunication or DataChannel whatever you want.
let localmessageInput = document.getElementById("local");
let localMessageArea = document.getElementById("localmsg");
localmessageInput.addEventListener("keyup", (event) => {
if (event.key === "Enter") {
addChildMessage(localMessageArea, event.target.value, "right");
localChannel.send(event.target.value);
event.target.value = "";
}
});
ENTER
is pressed.right
align.localChannel.send
function and pass our input value.Everything is same for remote peer part of code.
let config = null;
// config = {
// iceServers: [
// {
// urls: ["stun:stun1.l.google.com:19302", "stun:stun2.l.google.com:19302"],
// },
// ],
// iceCandidatePoolSize: 10,
// };
let localPeer = new RTCPeerConnection(config);
let remotePeer = new RTCPeerConnection(config);
RTCPeerConnection
object which provides methods to connect to a remote peer, maintain and monitor the connection. In our setting config is null
as our project is running in local enironment if you are connecting two peer over internet then you can use the commented config.Step define here are not proper way of signalling so please refer The WebRTC perfect negotiation pattern
async function init() {
localPeer
.createOffer()
.then((localOffer) => localPeer.setLocalDescription(localOffer))
.then(() => remotePeer.setRemoteDescription(localPeer.localDescription))
.then(() => remotePeer.createAnswer())
.then((remoteSDP) => remotePeer.setLocalDescription(remoteSDP))
.then(() => localPeer.setRemoteDescription(remotePeer.localDescription))
.catch((err) => window.alert(err));
}
createOffer()
this will return SDP of localPeer.localOffer
in the localPeer
by calling setRemoteDescription
.setRemoteDescription
for the remotePeer
.createAnswer()
which will create the Description but also initiate the connection process.localOffer
we can also share remoteSDP
by any proccess. Here we're just passing it to localPeer
.If you didn't understand the proccess above then you can refer flow diagram from here.
localPeer.onicecandidate = ({ candidate }) => remotePeer.addIceCandidate(candidate);
remotePeer.onicecandidate = ({ candidate }) => localPeer.addIceCandidate(candidate);
let localChannel = localPeer.createDataChannel("chat");
let remoteChannel = null;
localChannel.onmessage = (event) => {
log("Got message at local side", event);
addChildMessage(localMessageArea, event.data, "left");
};
remotePeer.ondatachannel = (event) => {
remoteChannel = event.channel;
remoteChannel.onmessage = (event) => {
log("Got msg at remote side", event);
addChildMessage(remoteMessageArea, event.data, "left");
};
};
createDataChannel
on localPeer creates a new channel linked with the remote peer, over which any kind of data may be transmitted.onmessage
is an event handler which specifies a function which is called when the a meessage is sent over the DataChannel by other peer.ondatachannel
is an event handler which specifies a function which is called when an RTCDataChannel
is added to the connection by the remote peer calling RTCPeerConnection.createDataChannel
.