How do I use RtcPeerConnection to connect with other instances? - javascript

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.

Related

How to make Web RTC work behind Symmetric NAT?

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?

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.

Sequence of offer/answer in WebRTC P2P mesh topology

I am using a web socket server (node JS) to act as a signaling server. I want to create a P2P mesh network - everyone is connected to each other.
What would be the order of operations/events when:
The 1st peer joins
The subsequent peers join
Do every connecting peer send an offer to the signaling server that is then sent to all other connected peers? I am unsure whether this is the right approach.
If what you are trying to do is create a mesh network with audio and video flowing at all times to everyone via a mesh network then I'd suggest reconsidering. Getting this to more than 3 or 4 users to work well is hard to impossible to achieve. See here for an explanation of what goes on the network for different network topologies: http://testrtc.com/different-multiparty-video-conferencing/
If you are trying to do this to get data connected (non voice or video), then you can do that, but again, the number of peer connections you can place in a single browser is limited and each one you add puts some overhead. Today, I wouldn't try to get this over 40 or 50 at most.
By your question, I am assuming all clients will be connected to your server via a WebSocket. You will probably end up routing messages through the server and creating peer connections judiciously when you want to connect a specific user to another one - or a small group of users together.
For the group scenario, I'd use an SFU model - look at Jitsi (https://jitsi.org/Projects/JitsiVideobridge) and Kurento (http://www.kurento.org/) for possible alternatives.
WebRTC connects client to client. If your client connects to at least one other - the other can have a list of others, as the others also has a list of as many others they possible can know. Then it propagates fast. The topology in the list is up to you. Signaling is not needed, because the information to connect with others is in the list.

webrtc streaming video only one side

I'm trying to make a video chat application based in http://www.html5rocks.com/en/tutorials/webrtc/basics/
It works well in the same network,
but when I try other networks have the following problem:
If i invite other user, only the other user can see the streaming
If he invites me, only he can see the streaming
I think the problem is in iceservers,
I'm using [{'url':'stun:stun.l.google.com:19302'}]
Any ideas on how can I solve this issue?
According to the same article that you mention:
If UDP fails, ICE tries TCP: first HTTP, then HTTPS. If direct connection fails—in particular, because of enterprise NAT traversal and firewalls—ICE uses an intermediary (relay) TURN server. In other words, ICE will first use STUN with UDP to directly connect peers and, if that fails, will fall back to a TURN relay server.
If I'm not mistaken, you use only STUN server. In this case if direct connection is not possible, and without TURN server, which acts as a relay, it's not possible to establish two-way connection.
Check out this article about how to set up your own TURN server, and about all this STUN and TURN stuff: http://www.html5rocks.com/en/tutorials/webrtc/infrastructure/
I solve the problem creating my own server STUN/TURN, now is working two-way connection.
Alaershov thank you for your help.

Categories