0

I am trying to understand how to use WebRTC to establish P2P connections following the example here: https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Simple_RTCDataChannel_sample. In my mind, it seems like when I create a new RTCPeerConnection, that connection should contain information about my public ip and subnet, so that when I create an offer and pass it to the remote computer, the remote would then have the details about where to send the response offer. However, whenever I try creating an RTCPeerConnection, it has 0.0.0.0 in it and no mention of my ip (which is not 0.0.0.0). Do you know why this could be? What am I doing wrong? How do I get it to display my public ip?

var localConnection = new RTCPeerConnection({
    'iceServers': [
        {
            'urls': ['stun:stun.l.google.com:19302'],
        },
    ],
});

var offer = localConnection.createOffer();
await localConnection.setLocalDescription(offer);

console.log(localConnection.localDescription);

// RTCSessionDescription { type: "offer", sdp: "v=0\r\no=mozilla...THIS_IS_SDPARTA-97.0 5252435491124817570 1 IN IP4 0.0.0.0\r\ns=-\r\nt=0 0\r\na=sendrecv\r\na=fingerprint:sha-256 94:95:5B:C2:D2:DC:56:71:EF:D6:A6:3E:CB:07:09:B0:A3:DB:FD:0B:8D:80:96:8C:56:B6:72:84:F3:36:1A:04\r\na=group:BUNDLE 1\r\na=ice-options:trickle\r\na=msid-semantic:WMS *\r\nm=application 9 UDP/DTLS/SCTP webrtc-datachannel\r\nc=IN IP4 0.0.0.0\r\na=sendrecv\r\na=ice-pwd:e4ae4077195d25ff0973d2c70c50111f\r\na=ice-ufrag:efc13e32\r\na=mid:1\r\na=setup:actpass\r\na=sctp-port:5000\r\na=max-message-size:1073741823\r\n" }
kloddant
  • 1,026
  • 12
  • 19
  • For anyone else viewing this question in the future looking for information on webrtc, I found the most bare-bones example here: https://github.com/lesmana/webrtc-without-signaling-server – kloddant Jan 30 '22 at 04:43

1 Answers1

3

The ICE Candidates haven't been gathered yet. It starts after SetLocalDescription is called, and candidates will be added to your localDescription as they arrive.

Set onicecandidate and it will called with your 'public ip'.

Sean DuBois
  • 3,972
  • 1
  • 11
  • 22
  • Thank you! I'll test this out tomorrow or Sunday. If it works, I'll mark this as accepted and be forever grateful. I'd given up on using webrtc for stuff, but if you've actually solved this problem, then it opens up whole new realms of distributed computing stuff that I have wanted to experiment with for years. – kloddant Jan 29 '22 at 06:31
  • Okay, I've tried this, and if I console.log(e.candidate), I get 3 requests similar to this: RTCIceCandidate { candidate: "candidate:0 1 UDP 2122252543 ee46912d-3d3f-469b-bc00-d943dc07ae3d.local 49276 typ host", sdpMid: "0", sdpMLineIndex: 0, usernameFragment: "16b023db" }. Does this string contain my ip address encoded somehow? But if that is the case, then why did I not see any outgoing requests in the network inspector? Are the outgoing requests happening but just not being shown by Firefox? – kloddant Jan 29 '22 at 15:30
  • Ah okay, so I see now that there are multiple ice candidates generated, and one or two of the others do have my public ip. I'm still confused as to why I don't see the outgoing requests to the STUN server and the incoming responses from the STUN server in the network inspector, but it must be happening. – kloddant Jan 29 '22 at 23:41
  • 1
    The network inspector in the browser doesn't show WebRTC traffic. Wireshark and/or tcpdump are great tools for inspecting WebRTC traffic though! – Sean DuBois Jan 30 '22 at 02:41
  • Thanks for letting me know. That was puzzling. – kloddant Jan 30 '22 at 04:42