39

I've been reading about WebRTC and it looks very promising. I wanted to make a simple lan game that automatically connects people on the same network. Though I could find people asking about something similar, and answers telling them it was possible, I couldn't find any clear tutorials on how it can be done.

I am however very new to WebRTC and web programming in general. So maybe I just don't know what to search for.

So, how can I automatically connect people using WebRTC on a local network without anyone having to run a separate server or connect to the internet? They should be able to open a html file on their computer and connect to everyone else doing that on the same network, even when there's no internet.

Thanks!

The Oddler
  • 6,314
  • 7
  • 51
  • 94
  • 6
    WebRTC is [not a discovery mechanism](http://stackoverflow.com/questions/29032884/why-signaling-server-needed-for-webrtc/29056385#29056385), unless you count what [sharedrop.io](https://www.sharedrop.io) does, which ingeniously uses WebRTC to find people who connect to it who are on the same lan, and then orchestrates lan-only file transfer between them. Why the "no Internet" restriction? Unless you're in a cabin in the woods, i would try something like that. – jib Aug 24 '15 at 18:03
  • @jib I tried sharedrop and that seems to work very nicely. Is there an explanation of how they did their discovery on lan? – The Oddler Aug 24 '15 at 19:51
  • 2
    It probably groups requests by external ip address, which is going to be the same from all machines on your lan (e.g. your router's external ip). Additionally it knows each machine's local ip address through WebRTC. – jib Aug 24 '15 at 21:06

3 Answers3

24

At least one machine needs to be a server, in the sense that it needs to have a port open that it listens on. This is a fact of life with all connections; when one machine opens a connection, there needs to be another machine on the other end that responds. Without this, no connections can ever be made.

If you are willing to have one or all machines listening on a port, then you can setup WebRTC on a LAN. In this case, you will not need STUN or TURN because there is no NAT traversal.

WebRTC does not need STUN or TURN on a LAN. WebRTC endpoints can generate local ICE candidates using their known addresses on the LAN. These get exchanged through signaling, either in directly in the SDP, or as ICE candidates in trickle ICE. The peer connection can be setup without ever needing to contact a STUN server external to the LAN.

Oxcug
  • 6,524
  • 2
  • 31
  • 46
mattm
  • 5,851
  • 11
  • 47
  • 77
  • 1
    Aha, makes sense. One local player being the host is fine. How can this be done? All tutorials I seem to find assume some remote server using STUN/TURN rather than a local host. – The Oddler Aug 24 '15 at 19:47
  • 1
    @TheOddler Start from an existing server implementation and setup on your LAN. Peers should be able to connect on the LAN without STUN/TURN. Once you have that working, you can figure out how to replicate the server functionality with your own code. It's basically a matter of passing SDP messages between the peers you want to connect, which will depend on how your application is supposed to function. – mattm Aug 25 '15 at 13:13
  • But, it's my understanding, using STUN or TURN servers will ensure the remote host has ports open for the connection and client host will receive ICECandidates with the information on how to connect. Without this step, how I can configure one host to connect to another, ensuring the target host has opened ports for the connection? Assuming javascript, some code or reference to classes would help. – ceztko Sep 01 '18 at 13:33
  • 5
    I answer myself: in local network ICE candidates generated locally must be broadcasted and added to all the other hosts with `addIceCandidate`. Very good example [here](https://github.com/emannion/webrtc-audio-video.git) – ceztko Sep 05 '18 at 14:18
  • Not really satisfied with this answer. Does anyone know if, using a STUN server on the internet, you can use WebRTC to communicate between two machines on the same LAN without that traffic leaving the LAN itself? – David Sep 23 '19 at 15:48
  • 1
    @David Your WebRTC endpoints can generate local ICE candidates without the STUN server, which you send via signaling. Presumably on your LAN, these candidates are sufficient to connect. The STUN server is not needed because you are on your LAN. – mattm Sep 23 '19 at 15:53
  • @mattm Thanks, I also found this: https://stackoverflow.com/questions/47064546/webrtc-after-signaling-on-lan. I was trying to find out if an "optimal" route was negotiated. – David Sep 23 '19 at 15:56
  • @David You can ensure traffic does not leave the LAN by not using addresses outside the LAN. If you resolve addresses via STUN, you get an external address. So don't use STUN. – mattm Sep 23 '19 at 16:00
8

WebRTC cannot work without some sort of signaling mechanism. Basically, your clients need to know at least something about each other, and this 'something' in the terms of WebRTC is an SDP package (Session Description Protocol). After they exchange SDP packages, WebRTC engine will try to connect clients in the most direct way.

Try this article: http://www.html5rocks.com/en/tutorials/webrtc/basics/

It will give you basic understanding of how WebRTC works, and you will answer your question yourself. Keywords: signaling, STUN and TURN.

Good luck!

Alexey Ershov
  • 176
  • 1
  • 4
6

Before two peers could establish direct connection, they both should exchange with a set of data (network parameters, media configuration, usable protocols, etc.), so they would figure out how to connect to each other. They can do this process with using SDP (Session Description Protocol).

Hence, you need a signaling server in the network that would be reachable by every potential peer. When a client wants to connect to the 'WebRTC network', it should first connect to the signaling server. Then, signaling server will notify other peers that we have a new one, and all peers will exchange data with SDP via the signaling server. After that, peers would be able to establish direct connection to the new peer. When direct connection is established, all data goes between peers directly.

fycth
  • 3,410
  • 25
  • 37