How to make Web RTC work behind Symmetric NAT? - javascript

I've created 2 simple webpages (for peer 1 and peer 2) that exchange a few messages with WebRTC data channel. You need to manually copy-paste offer and answer between the webpages. After the answer is pasted back to peer 1 the connection is established and those messages are sent and received. But, it only works if both peers are behind normal NAT.
Therefore I tried to use TURN server. I tried self-hosted Coturn server and Xirsys turn service, but just couldn't get them to work.
The web pages on codesandbox are:
Peer 1: https://codesandbox.io/s/web-rtc-peer-1-shared-zl09e
Peer 2: https://codesandbox.io/s/web-rtc-peer-2-shared-9z40r
The sandboxes now have fake credentials for turn servers, but I used real ones (static) in my experiments.
So, if I use this sandboxes in symetric NAT, or I just force the use of turn server with config iceTransportPolicy: "relay", then it does not work - the connection does not open.
Question 1: What do I have to do to make it work?
Question 2: Can you make it work with your own TURN server (and iceTransportPolicy: "relay" config option?

Related

How does RTCPeerConnection use the array of STUN/TURN servers?

We are doing some research regarding the behavior of Web RTC with the list of STUN/TURN servers provided. I cannot find any documentation so I am doing some tests but I hope someone can provide a clear explanation.
Following the documentation (https://developer.mozilla.org/en-US/docs/Web/API/RTCIceServer/urls) we can provide any number of servers.
But how does the RTCPeerConnection choose the server to use?
Does it try the first one and if it fails try the second one until one works?
Should all the servers be up and running or is the connection able to skip an unreachable server?
If the first server is able to conclude the negociation, does it still try with the remaining servers?
Does it simply change the list of candidates?
To provide more context, we have a working WebRTC application using the Google Stun Servers (stun.l.google.com:19302) however we are migrating on our own STUN servers. We have an API that returns the list of STUN servers to use however based on the behavior we might provide a different list.
Thanks for your help
But how does the RTCPeerConnection choose the server to use?
The RTCPeerConnection doesn't choose a server it choose a pair of ICE candidates. A ICE candidate is generated by contacting the servers.
Does it try the first one and if it fails try the second one until one works?
It contacts them all (this process is called gathering). Your WebRTC implementation may stop gathering when a connection is established.
Should all the servers be up and running or is the connection able to skip an unreachable server?
It is ok for a server to be down. Trickle ICE allows connectivity checks to proceed if all of them are not working.
If the first server is able to conclude the negociation, does it still try with the remaining servers?
Two WebRTC Agents don't communicate through a STUN server, this question has a little more nuance. In the connectivity chapter of WebRTC for the Curious check out how ICE goes through steps.
Does it simply change the list of candidates?
Yea! For each STUN server you may have another candidate. This depends on the behavior of your NAT. You could be behind a NAT configuration that only gives you one mapping. Unlikely but still possible!

Web P2P/WebRTC serverless signalling: is it possible to signal successfully without generating a response?

My goal is to create a p2p serverless web application and I'm not completely clear on the possibilities yet. I figured there was a need for peer discovery and that there would be NAT issues. After that, I searched on Google and read a lot of articles, I came to the conclusion that WebRTC is my only bet. However, there is a thing that I don't understand:
Why is it not possible to create a p2p connection through simply sending a sharable link with connection info in the GET parameters of the URL that only contains the offer? If Alice sends her public IP (and whatever else is in the offer) to Bob via example.com/?info=<IP_ADDRESS>&info2=<OTHER_STUFF>, then why can't Bob immediately connect to Alice? Why does Bob need to send a response to a signalling server?
This answer seems to suggest it's not possible: Establishing WebRTC peer connection
But I don't get why, there should be enough info. Is it a trust issue?
Is it possible with any technology (i.e. not only WebRTC) to create a p2p application in which only Alice sends her info and Bob can respond back to Alice by using the IP-address of Alice?
I implemented the following to get p2p working https://github.com/chr15m/bugout
He markets it as server in the browser, but it's also for peers in the browser, or clients, or any code that wants to communicate to another browser for whatever reason.
How it works: it uses open webtorrent trackers to create a peer discovery mechanism. By doing this, one does not need to implement their own signal server as they're hitching a ride from open-source infrastructure. Though, he also implemented his own signaling server. You also get STUN servers for free by doing this.

Sending text information in a p2p network

I have been attempting to create a simple peer to peer network for quite some time.
I stumbled upon a javascript example found here that I have been working from.
When I run the example found in the repository, the peers never make a connection. I am beginning to wonder if there are problems with the ports I am attempting to use. When I use a port checker online, I can see that most of my ports are stealthed.
Questions:
Do my ports need to be open (not stealthed) to communicate on a P2P network?
How would I go about opening ports?
I have started an issue that describes my problem in more detail found here

How do I use RtcPeerConnection to connect with other instances?

I have followed the tutorials on http://www.html5rocks.com/en/tutorials/webrtc/basics as well as watch some of the videos but I am so confused. I was able to get 2 peerConnections working on a single page but I have no Idea how build on this to actually allow users on different machines/networks or what ever to actually connect.
From what I can see it involves using socket io and I have found plenty full frameworks and apps doing it but I am struggling to understand it.
Can any one point me to any tutorials that would help me to go from this rtcPeerConnection to communicating with other instances
Any help is greatly appreciated
To allow 2 users establish p2p connection, one of the peer should send "description" (ip address, port, etc) to second peer. Second peer sets it as remote description using setRemoteDescription method of RTCPeerConnection. Second peer invokes createAnswer and sends its localDescription to first peer.
When both peers know about each other, connection can be established. I think that tutorial mentioned by you in first link is comprehensive.
To allow 2 peers exchange "descriptions" you can use any hand-written signalling server. For example client can poll web-server using http.

How can I implement simple serverless p2p browser to browser messaging with minimal overhead?

I'm trying to create some basic implementations of simple games (tic tac toe is the starting project) which can be played over the internet without requiring a central server. The page would not even need to be hosted and could be run locally on the machine, or it could be hosted on a web server. When hosting the game, the page would inform the host of his IP address, which could then be sent by any method (phone, instant message, etc) to a friend. That friend would type or copy/paste the IP into a join dialog and be able to play the game in question. I would like these 2 parties to be able to do this without installing any additional software, and without contacting a central server of any kind.
I have looked into many potential solutions involving node.js, webrtc, websockets, flash, java, etc. Each one of these has a problem associated with it, such as requiring a central server, or requiring the client to potentially have to download something that isn't already installed on their computer, or only transferring audio and video and not being useful for sending data messages. It may seem trivial to tell someone that they need to download java.. or for me to develop the application with flash, but that is all contrary to my ultimate goals.
If it just isn't possible to do what I'm trying to do entirely in javascript, then it just isn't possible. But I don't see why it couldn't be, considering that browsers are capable on their own of sending and receiving text data to URLS which resolve to IPs or directly to IPs. Other solutions are welcome but if this isn't possible to do, it really should be.
The simple explanation of the exact requirements for what I'm trying to do is:
Should use entirely free (as in beer) technologies. (no flash, i realize that web apps for flash player can be coded for free, but peer to peer in stratum requires a signup for a beta key, which assuming i could obtain for free, wouldn't necessarily remain free forever.)
No external servers or false peer to peer. (again as in flash or unity based solutions where the imitation of peer to peer can be acheived, as long as you use their central server)
No client downloads (sure, most people have java or flash installed, but many don't, and java is a pretty hefty download and not friendly for computer illiterate users. It even tries to install toolbars now. On top of this, many of my users would not be willing to download anything at all, including java or unity. Which have their own issues relating to this project as already mentioned)
In summary, if ajax can send a request to a specified IP and listen for a response.. why can't i get simple peer to peer messaging in pure js? Or can I?
I shouldn't need to host a full blown web server or a seperate application or plugin of any kind to send and receive data.
Am I missing something?
After pubnub was recommended, I looked there and was partially impressed. However, I eventually stumbled across exactly what I was looking for UNBELIEVABLY. RTCDataChannel is the answer. This site finally showed that what I want is possible. The browser support for this functionality is small but growing and the entire ordeal has strengthened my faith in the growing support for peer to peer applications in the browser community.
In summary, if ajax can send a request to a specified IP and listen
for a response.. why can't i get simple peer to peer messaging in pure
js? Or can I?
It's due to the fact that an ajax request must be handled by an HTTP server so you still need to install a server to every clients.
say you want
free
no external servers
no client downloads
I would say it is impossible to archive with all of these requirements except that you cut one of them off.
My suggestion is pubnub. This solution still need a server and it is not free(they have a free usage tier). But the good thing is you have an imitate p2p connection without doing server things and no client download needed.

Categories