localhostwebrtcrtcpeerconnection

Do WebRTC always connect locally when using peers with same IP adress?


I'm trying to understand a bit more about webRTC so:

  1. My guess is that when running an app from localhost and establishing a peerConnection with myself ( no firewalls, vpns or symmetric nats involved ), WebRTC will connect locally using 127.0.0.1 adress.
  2. Now if I run my app from the web my other guess would be that webRTC will be using my public ip adress to finally try to connect to my same public ip adress.

So my question is, when connecting from localhost I'm pretty sure WebRTC peerConnection doesn't go through internet to connect to myself, will this be the same with the second scenario?


Solution

  • Whether WebRTC goes through internet or not depends on what you pass in ICE server list when creating a peer. If you don't pass any ICE server (STUN or TURN), then WebRTC just gathers local ICE candidates (i.e. from localhost). If you pass at least one STUN or TURN server url, then WebRTC tries to gather ICE candidates using that servers. But even though you provide non-empty ICE server list, if both peers can reach each other using local candidates (of course, if you haven't disabled gathering local ICE candidates), then its likely that WebRTC would select them as the best candidates pair, because they have lower "network cost" for sending media than the candidates gathered from outside of localhost.