46

On an isolated LAN, is there any way that a WebRTC connection can be made simply with the IP addresses assigned by the DHCP server?

I understand that I can accomplish this with Node.js and Socket.io - but I was really hoping to avoid setting up that kind of server with my limited skill set. I'm a science teacher who dabbles in programming, so feel free to keep it simple. Thank you!

UPDATE

Alex, you are correct that I can avoid using a STUN server if all of the computers are on the same local network. Although I had to bite the bullet and install Node.js on my laptop, it was really wasn't complicated. I then tried a whole bunch of 'working examples' that didn't work for me, until I found this one and his GitHub files.

After running the server script in Node, I had a DataChannel connection between two browser windows on the same machine, but not between different computers. I edited the .html files to point to my local server IP address instead of localhost and I could then connect with multiple computers. Then came the real test - could I use this without an internet connection? I found the line that specified using Google's STUN servers and changed it from

var config = {"iceServers":[{"url":"stun:stun.l.google.com:19302"}]};

to just

var config = {"iceServers":[]};

It worked. :-)

pdoherty926
  • 9,895
  • 4
  • 37
  • 68
brainBanter
  • 491
  • 1
  • 4
  • 8
  • if all clients are inside local network, I think you can avoid stun and turn servers. At least it was working so with my application and I added stun when I need to connect outside. You can check: https://chatroomone.azurewebsites.net/ – Alexan Jun 09 '15 at 21:24
  • but do you really need video? if you need just real-time text chat you can use SignalR instead WebRTC. – Alexan Jun 09 '15 at 23:00
  • @Alex - Thank you for the suggestion, but SignalR seems to require a server set-up too. I may have to bite the bullet and set up a local server... but I was captivated by the allure of 'peer-to-peer' 'browser-to-browser' simplicity.I just want a user on the LAN to be able to type something on one local page to show up on another local page. – brainBanter Jun 10 '15 at 00:18
  • I think you need web server anyway. – Alexan Jun 10 '15 at 02:17
  • @Alex - Yes... I have a web server to host the page. What I was trying to avoid was setting up Node.js or some other type of server-side programming to handle input from the various clients on the LAN. I can do it, I know... but I would have to take the (precious) time to learn server-side scripting. webRTC seemed like it was mostly implemented by client-side Javascript, which I am familiar with. But in everything that I've researched, it had the caveat of having to have some sort of server to make the inital connection. I was hoping that could be done by my wireless router. :-/ – brainBanter Jun 10 '15 at 02:23
  • but if you need just real-time communication with your students, you can use Google hangout, for example. – Alexan Jun 10 '15 at 02:23
  • yes, I think for WebRTC signaling you need some server part: http://www.html5rocks.com/en/tutorials/webrtc/basics/#toc-signaling I just thank that SignalR is much simpler and you can download sample: http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr, but you need web server, which support ASP.NET, IIS for example. – Alexan Jun 10 '15 at 02:30
  • probably you need something like this: https://mywebchat.azurewebsites.net, you can use it. – Alexan Jun 10 '15 at 02:57
  • or maybe this: http://signalrtc.com/ – Alexan Jul 05 '15 at 01:30
  • yes, you don't need actually to install STUN servers, you can use existing: https://gist.github.com/zziuni/3741933 – Alexan Jul 07 '15 at 01:19
  • I know nothing about Node.js, I know you can use for signaling socket.io or SignalR. – Alexan Jul 07 '15 at 01:26
  • Certainly happy to provided a nice self-contained rtc.io example that should work nicely in that environment. It's certainly an interesting use case so it would be nice to make something that "just works" in that kind of situation. – Damon Oehlman Aug 16 '15 at 10:20
  • 2
    Maybe you could add your solution as an answer, and mark this as done :D? – timiTao Apr 07 '18 at 08:07
  • The HTML link in your description (this link) is not available now. Github link is available. But not able to understand from the code in the gibhub, what exactly needs to be done. – zolio Jan 21 '22 at 08:19

2 Answers2

14

Omit the iceServers list:

const pc = new RTCPeerConnection();

Either omit the iceServers list from the RTCPeerConnection constructor, or make it the empty list [].

From RTCPeerConnection docs:

iceServers | optional

An array of RTCIceServer objects, each describing one server which may be used by the ICE agent; these are typically STUN and/or TURN servers. If this isn't specified, the connection attempt will be made with no STUN or TURN server available, which limits the connection to local peers.

webrtc/samples demo

The WebRTC project has a Trickle ICE sample that you can use to see how changes in iceServers effect the candidate address that are gathered. The specific sample you want to look at is.

  1. Run it with defaults set by pressing the Gather candidates button at the bottom of the page. This will return a list of addresses which include the address of the public side of your NAT.

    Default ICE Servers

  2. Now remove all the ICE servers from the list and press Gather candidates again, this time you should only see local network addresses.

    No ICE Servers

Notice that, on my network, the 2 public IPv4 addresses (beginning with 98.) only appear when I'm using the default ICE servers. When I use an empty ICE server list, my public IPv4 addresses are no longer discovered. My IPv6 addresses, on the other hand, are the same in both tests because they aren't subject to NAT.

Here is a link to the source code that sets up iceServers and PeerConnection.

Doug Richardson
  • 10,483
  • 6
  • 51
  • 77
  • 2
    Browsers generally don't provide default ICE servers anymore, so passing in an empty `[]` shouldn't be needed. I've updated the [MDN](https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/RTCPeerConnection#RTCConfiguration_dictionary) article to reflect this. Sorry for any confusion. Firefox does still have an `about:config` pref to enable default ICE servers, which is probably what MDN was referring to, but this is non-standard, and probably shouldn't influence MDN and general web programming advice. The sample provides its own servers; they're not "default" ones. – jib Aug 28 '19 at 19:20
  • 1
    Simply calling `const pc = new RTCPeerConnection();` should suffice. If you wanna update your answer I'll upvote it to get this long-standing self-answered question off the top unanswered webrtc questions list. ;) – jib Aug 28 '19 at 19:28
  • 1
    Oh, I haven't used webrtc in a couple years. That must have changed. So now you're saying you have to call getDefaultIceServers explicitly for that behavior? https://www.w3.org/TR/webrtc/#dom-rtcpeerconnection-getdefaulticeservers – Doug Richardson Aug 28 '19 at 19:41
  • Yup, I missed that they were explicitly setting the server in the webrtc sample code I linked to. I'll update the answer. – Doug Richardson Aug 28 '19 at 19:43
  • 2
    [getDefaultIceServers()](https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-getdefaulticeservers) is marked "Feature at risk" due to lack of implementations. The Firefox `media.peerconnection.default_iceservers` pref works as you originally described, it's just non-standard and rarely used, so I wouldn't worry web developers about it. Especially since the feature isn't an obstacle to connecting over LAN. – jib Aug 29 '19 at 00:11
  • Zoom video calls are using WebRTC without STUN / TURN servers, does it mean that they are using getDefaultIceServers() to get a default STUN server? https://webrtchacks.com/zoom-avoids-using-webrtc/#datachannels – baptx Dec 13 '20 at 14:54
3

Alex, you are correct that I can avoid using a STUN server if all of the computers are on the same local network. Although I had to bite the bullet and install Node.js on my laptop, it was really wasn't complicated. I then tried a whole bunch of 'working examples' that didn't work for me, until I found this one and his GitHub files.

After running the server script in Node, I had a DataChannel connection between two browser windows on the same machine, but not between different computers. I edited the .html files to point to my local server IP address instead of localhost and I could then connect with multiple computers. Then came the real test - could I use this without an internet connection? I found the line that specified using Google's STUN servers and changed it from

var config = {"iceServers":[{"url":"stun:stun.l.google.com:19302"}]};

to just

var config = {"iceServers":[]};

brainBanter
  • 491
  • 1
  • 4
  • 8