simple-peer icon indicating copy to clipboard operation
simple-peer copied to clipboard

is it impossible to make a simple webrtc connection reliably!?

Open farzher opened this issue 3 years ago • 7 comments

  simplepeer.min.js:6 Uncaught Error: Connection failed.
  at p._onConnectionStateChange (simplepeer.min.js:6)
  at RTCPeerConnection._pc.onconnectionstatechange (simplepeer.min.js:6)

happens in all browsers, on all my computers, all of my friends computers, all webrtc projets, and even on localhost. usually it connects fine, but sometimes it just gets stuck.

this is not a bug with simple-peer, this is a bug with webrtc. i came here expecting simple-peer to have retries built in or something to solve this. am i the only person with this issue or does everyone just accept it??? it's so annoying and has been happening with many projects for many years now. .

even your simple demo where you copy/paste the signals will fail sometimes if you do it enough times. image

i dare you to try any webrtc demo about 20 times, it will get stuck.

sorry for venting my frustrations here but i've been working on a webrtc project off and on for about 3 years now and i still can't connect reliably. any help is appreciated

farzher avatar Feb 05 '21 00:02 farzher

Getting this error on some of my projects. I'm also looking for some help.

vj-abishek avatar Feb 05 '21 02:02 vj-abishek

I'm interested in learning about this error and getting it fixed

usama-govsoft avatar Mar 13 '21 22:03 usama-govsoft

my dumb fix is to just keep calling rtcpeer.restartIce() every few seconds until it's connected.

i've noticed different behavior on different domains. same exact webrtc code. i don't know why the domain is relevant at all. yet the connection process is totally different and more reliable on different domains.

i don't think it's worth looking into too much, besides catching the error and retrying. since is probably a bug in the browser iteslf.

farzher avatar Mar 13 '21 23:03 farzher

The "fix" is to use a TURN server.

My experience with 4 professional WebRTC projects so far:

  • Before you have a TURN server: everything works fine on your machine initially. But then it just doesn't and doesn't work on someone else's, or on mine with particular peers. Constant hand-wringing, "how can we make this more reliable???", googling, restart hacks, ...
  • After you finally added that TURN server: it just works** and you move on to other issues

After the 3rd time I simply stopped being anxious about connection issues before there was a TURN server, it's not worth it.

**) OK, just works is too strong. You still have to figure out restarts when internet connections are dropped, like when a smartphone goes black and freezes its apps, and you want to recover as soon as your page becomes visible again.

mitschabaude avatar Mar 18 '21 10:03 mitschabaude

do you know what a TURN server is? that's literally the "we failed to connect, let's just give up on p2p and make a normal laggy server relay connection"

no, that's not a solution i want. especially when it fails to connect at random and uses the TURN server at random. that was actually one of the biggest bugs i had in my app. was why the heck i'm getting 70ms ping on a localhost connection. works way better now that i removed the TURN server

farzher avatar Mar 18 '21 17:03 farzher

Ok, yeah sure I know what it is and I totally get where you're coming from. Just wanted to share my experience ;)

FWIW, if the TURN server slows down connections that don't even need one, this may mean you need to enable trickle ICE? Then you don't have to wait for relay candidates (the ones from the TURN server) before connecting. Localhost should be fast then.

If, on the other hand, the choice is between no connection at all or a laggy one, you'll likely prefer the first option in most use cases.

mitschabaude avatar Mar 18 '21 19:03 mitschabaude

I've been studying it since 2018 on my spare time, I saw some people moving to native WebRTC browser API the problems are the same but since you control the code you might not fall into design decisions that are particular to simple-peer.

Honestly, I think simple-peer would be a better lib if it could add CHANGELOG file for release notes, it helps us keep up to date on it overtime. But, I love it and I'm grateful cause I've played a lot with WebRTC with it and I get it is hard for maintainers to keep a popular side project like this one.

I haven't built any corp program with it cause I don't feel confident using it on comercial products, so this is my toy lab because I just never know when it breaks and it can refuse to connect for many many reasons far away from my ability to understand it hahaha so yeah shout out for you all people, good luck with p2p in 2022 and share all your gotchas publicly so I can find it later and use it as well hopefully.

felquis avatar Dec 24 '21 17:12 felquis