WebRTC archicture
Network Adress translation
It is the process of mapping IP:port to pairs to anther IP:port
10.0.0.2:5555
4.4.4.4:80
10.0.0.1:5555
Router, Gateway, Firewall, etc
| 8889 | 10.0.0.2 | GET / | 4.4.4.4 | 80 |
|---|
10.0.0.2:5555
4.4.4.4:80
10.0.0.1:5555
Router, Gateway, Firewall, etc
| 8889 | 10.0.0.2 | GET / | 4.4.4.4 | 80 |
|---|
10.0.0.2:5555
4.4.4.4:80
10.0.0.1:5555
Router, Gateway, Firewall, etc
| 5649 | 5.5.5.5 | GET / | 4.4.4.4 | 80 |
|---|
| Internal IP | Internal Port | Ext. IP | Ext. port | Dest IP | Dest Port |
|---|---|---|---|---|---|
| 10.0.0.2 | 8889 | 5.5.5.5 | 5649 | 4.4.4.4 | 80 |
10.0.0.2:5555
4.4.4.4:80
10.0.0.1:5555
Router, Gateway, Firewall, etc
| 80 | 4.4.4.4 | ok | 5.5.5.5 | 5649 |
|---|
| Internal IP | Internal Port | Ext. IP | Ext. port | Dest IP | Dest Port |
|---|---|---|---|---|---|
| 10.0.0.2 | 8889 | 5.5.5.5 | 5649 | 4.4.4.4 | 80 |
10.0.0.2:5555
4.4.4.4:80
10.0.0.1:5555
Router, Gateway, Firewall, etc
| 80 | 4.4.4.4 | ok | 5.5.5.5 | 5649 |
|---|
| Internal IP | Internal Port | Ext. IP | Ext. port | Dest IP | Dest Port |
|---|---|---|---|---|---|
| 10.0.0.2 | 8889 | 5.5.5.5 | 5649 | 4.4.4.4 | 80 |
10.0.0.2:5555
4.4.4.4:80
10.0.0.1:5555
Router, Gateway, Firewall, etc
| 80 | 4.4.4.4 | ok | 10.0.0.2 | 8889 |
|---|
| Internal IP | Internal Port | Ext. IP | Ext. port | Dest IP | Dest Port |
|---|---|---|---|---|---|
| 10.0.0.2 | 8889 | 5.5.5.5 | 5649 | 4.4.4.4 | 80 |
10.0.0.2:5555
4.4.4.4:80
10.0.0.1:5555
Router, Gateway, Firewall, etc
| 80 | 4.4.4.4 | ok | 10.0.0.2 | 8889 |
|---|
| Internal IP | Internal Port | Ext. IP | Ext. port | Dest IP | Dest Port |
|---|---|---|---|---|---|
| 10.0.0.2 | 8889 | 5.5.5.5 | 5649 | 4.4.4.4 | 80 |
v=0
o=- 6542080654249767232 2 IN IP4 127.0.0.1
s=-
t=0 0
a=group:BUNDLE 0
a=extmap-allow-mixed
a=msid-semantic: WMS
m=application 9 UDP/DTLS/SCTP webrtc-datachannel
c=IN IP4 0.0.0.0
a=candidate:2781379592 1 udp 2113937151 5da2f2ca-2797-46f3-bed7-a825abb3a884.local 65093 typ host generation 0 network-cost 999
a=ice-ufrag:mpr1
a=ice-pwd:IpYJsifvS1E5/gDGbyCWWfWp
a=ice-options:trickle
a=fingerprint:sha-256 48:9B:5F:6A:F7:AB:57:48:49:05:41:44:0D:3C:A7:55:C6:A2:BE:3F:06:AE:03:26:0F:84:57:FC:01:7C:3A:3C
a=setup:actpass
a=mid:0
a=sctp-port:5000
a=max-message-size:262144// creates a local RTC connection
const localConnection = new RTCPeerConnection();
// fires when we get a new ICE canidate, this the moment when we need to share the SDP
localConnection.onicecandidate = (e) => {
console.log(' NEW ice candidate !! on localconnection reprinting SDP ');
console.log(JSON.stringify(localConnection.localDescription));
};
// creating a simple data channel to chat peer to peer
const sendChannel = localConnection.createDataChannel('sendChannel');
sendChannel.onmessage = (e) => console.log('messsage received!!!' + e.data);
sendChannel.onopen = (e) => console.log('open!!!!');
sendChannel.onclose = (e) => console.log('closed!!!!!!');
// creating an SDP offer, then set is as our local SDP
localConnection.createOffer().then((o) => localConnection.setLocalDescription(o));
//you can specify a STUN server here
const iceConfiguration = {};
iceConfiguration.iceServers = [];
//turn server
iceConfiguration.iceServers.push({
urls: 'turn:my-turn-server.mycompany.com:19403',
username: 'optional-username',
credentials: 'auth-token',
});
//stun server
iceConfiguration.iceServers.push({
urls: 'stun:stun1.l.google.com:19302',
});
const localConnection = new RTCPeerConnection(iceConfiguration);
// set offer with the SDP value from other peer using signaling via any meduim you want
const offer = '...'
const remoteConnection = new RTCPeerConnection();
remoteConnection.onicecandidate = (e) => {
console.log(' NEW ice candidnat!! on localconnection reprinting SDP ');
console.log(JSON.stringify(remoteConnection.localDescription));
};
remoteConnection.ondatachannel = (e) => {
const receiveChannel = e.channel;
receiveChannel.onmessage = (e) => console.log('messsage received!!!' + e.data);
receiveChannel.onopen = (e) => console.log('open!!!!');
receiveChannel.onclose = (e) => console.log('closed!!!!!!');
remoteConnection.channel = receiveChannel;
};
// here we set the other party SDP as our remote SDP and we do the same on the other party
remoteConnection.setRemoteDescription(offer).then((a) => console.log('done'));
//create answer which also trigger new SDP, we also must set the local SDP with that answer
await remoteConnection
.createAnswer()
.then((a) => remoteConnection.setLocalDescription(a))
.then((a) => console.log(JSON.stringify(remoteConnection.localDescription)));
//send the answer to the client
// set offer with the SDP value from other peer using signaling via any meduim you want
const offer = '...'
const remoteConnection = new RTCPeerConnection();
remoteConnection.onicecandidate = (e) => {
console.log(' NEW ice candidnat!! on localconnection reprinting SDP ');
console.log(JSON.stringify(remoteConnection.localDescription));
};
remoteConnection.ondatachannel = (e) => {
const receiveChannel = e.channel;
receiveChannel.onmessage = (e) => console.log('messsage received!!!' + e.data);
receiveChannel.onopen = (e) => console.log('open!!!!');
receiveChannel.onclose = (e) => console.log('closed!!!!!!');
remoteConnection.channel = receiveChannel;
};
// here we set the other party SDP as our remote SDP and we do the same on the other party
remoteConnection.setRemoteDescription(offer).then((a) => console.log('done'));
//create answer which also trigger new SDP, we also must set the local SDP with that answer
await remoteConnection
.createAnswer()
.then((a) => remoteConnection.setLocalDescription(a))
.then((a) => console.log(JSON.stringify(remoteConnection.localDescription)));
//send the answer to the client