question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Restart an existing SimplePeer connection

See original GitHub issue

I’m trying to “restart” an existing connection.

Assuming connection const peer = { first: new Peer(... args) }, I can’t recreate a new connection between the same two peers on peer.first without reloading the page (when reloading the page the connection is (re)established without any issue).

I have tried to the following:

// "first" connection - [displaying only the code of the initiator]
const peer = { userA: new Peer({ initiator: true, trickle: false }) };

// [omitted code up to `.connected = true`]

peer.userA.destroy(); // with and without the optional `error message`
destroy peer.userA; // both after calling `destroy()` and without calling `destroy()`

// "second" connection - [displaying only the code of the initiator]  
peer.userA = new Peer({ initiator: true, trickle: false });

Unfortunately the “second” connection won’t connect, both when the “first” connected, and when it did not.

The “first” connection in the console appears like this:

allowHalfOpen:false
answerConstraints:{}
channelConfig:{}
channelName:"7127337a05636fb1ba616871719788627bd22941"
config:{iceServers: Array(2)}
connected:true
constraints:{}
initiator:true
localAddress:"XXX.XXX.XXX.XXX" // correct IP
localPort:58308
offerConstraints:{}
readable:true
remoteAddress:"XXX.XXX.XXX.XXX" // correct IP
remoteFamily:"IPv4"
remotePort:61205
sdpTransform:ƒ (e)
streams:[]
trickle:false
writable:true
...

The “second” connection, however, is missing some crucial data (e.g. IP), both in the initiator and in the receiver. The initiator appears like this in the console:

allowHalfOpen:false
answerConstraints:{}
channelConfig:{}
channelName:"dfe7107551bf3ebf4e812b463882b5b52f6715ed"
config:{iceServers: Array(2)}
connected:false
constraints:{}
initiator:true
localAddress:undefined
localPort:undefined
offerConstraints:{}
readable:true
remoteAddress:undefined
remoteFamily:undefined
remotePort:undefined
sdpTransform:ƒ (e)
streams:[]
trickle:false
writable:true
...

The second receiver has also channelName:null.

Is it a but, am I missing something, or is it forbidden to reinitiate a connection between the same two peers without reloading the page?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
t-mullencommented, Jul 11, 2018

Here is the issue:

SimplePeer debugger:  signal() <--- What is this signaling data? Which peer (first or second), is it called on?
SimplePeer debugger:  destroy (error: %s) InvalidStateError: Failed to set remote answer sdp: Called in wrong state: kStable

You are calling .signal() on the userA side after the first peer object was destroyed but before the second even sends an offer.

The Error: Ice connection failed. is just because the userA side failed to connect.

It seems there’s quite a few problems with how you are signalling. Try to keep messages intended for a connection completely separate, so they don’t accidentally go to the wrong peer object. Signalling can go wrong in a lot of places when using multiple peers, especially if you are trying to use the same signalling channel.

I do have a signalling module that can give you basic channel separation and a connection model if you’re interested in using it or reading the code: https://github.com/RationalCoding/simple-signal

Closing as this doesn’t appear to be an issue in simple-peer, but feel free to continue discussion.

0reactions
iranagamecommented, May 4, 2021

Have you found a solution for this? I have a exact issue like this. @ilDon

Read more comments on GitHub >

github_iconTop Results From Across the Web

Close and restart peer · Issue #417 · feross/simple-peer - GitHub
You need construct a new peer object for each connection. Peers are one-to-one and one-use. You can't "reuse" peer objects.
Read more >
Is there a way to reconnect two peers after one is destroyed?
Do you create new Peer objects on both sides of the connection? Destroying a peer locally will destroy the remote peer it's connected...
Read more >
RTCPeerConnection.close() - Web APIs - MDN Web Docs
The RTCPeerConnection.close() method closes the current peer connection.
Read more >
simple-peer - Bountysource
As an optimization, ICE restart can be done preemptively when iceConnectionState == 'disconnected' (before 'failed' ) and it won't immediately bounce back to ......
Read more >
Simple-peer-server - npm.io
simplePeerOptions is optional. It exposes the options available when creating a new WebRTC peer connection via simple-peer. See simple-peer documentation for ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found