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.

Error: cannot signal after peer is destroyed

See original GitHub issue

What is the proper way to tear down the connection?

I establish a WebRTC connection using a SimplePeer object, but then I need to drop the connection and reconnect. I call the simplePeer.destroy(), create a new SimplePeer object and do the signalling part, but I get the following error:

Error: cannot signal after peer is destroyed

What am I doing wrong? Same issue as this one: https://github.com/feross/simple-peer/issues/101

Issue Analytics

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

github_iconTop GitHub Comments

4reactions
ahmdsdkcommented, May 8, 2022
        const connectionRef = useRef();

    const callUser = (id) => {
        setCallEnded(false);
        setReceivingCall(true);
        setCallAccepted(false);
        console.log(id)
        const peer = new Peer({
            initiator:true, 
            trickle:false, 
            stream: stream
        })
        // debugger;
        peer.on("signal", (data) => {
            socket.emit("callUser", {
                usedToCall: id,
                signalData: data,
                from: me,
                name: name 
            })
        })

        // Received a remote video stream, which can be displayed in a video tag
        peer.on("stream", (stream) => {
            userVideo.current.srcObject = stream;
        })

        socket.on("callAccepted", (signal) => {
            setCallAccepted(true); 
            peer.signal(signal); 
        })

        connectionRef.current = peer;
    }

    const answerCall = () => {
        setCallAccepted(true); 
        const peer = new Peer({
            initiator: false,
            trickle: false,
            stream: stream 
        })

        peer.on("signal", (data)=> {
            socket.emit("answerCall", {signal:data, to:caller});
        })

        peer.on("stream", (stream) => {
            userVideo.current.srcObject = stream;
        })

        peer.signal(callerSignal)
        connectionRef.current = peer;
    }

    const leaveCall = () => {
        setCallEnded(true);
        connectionRef.current.destroy();
    }

I am also doing the same thing but getting error. @olgeorge can you suggest where I am doing wrong. I don’t have much knowledge of class components so your code was not so helpful to me.

I faced the same issue you have to unsubscribe to the socket.io callAccepted listener when peer is closed:

peer.on('close', () => { console.log('peer closed'); socket.off("callAccepted"); });

3reactions
t-mullencommented, Jul 3, 2018

Do you create new Peer objects on both sides of the connection? Destroying a peer locally will destroy the remote peer it’s connected to.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Simple-peer. Cannot signal after peer is destroyed
I have simple video chat app which connects 2 peers using WebRTC, Socket.io and ... Uncaught Error: cannot signal after peer is destroyed....
Read more >
Uncaught Error: cannot signal after peer is destroyed
Uncaught Error : cannot signal after peer is destroyed.
Read more >
simple-peer on.("signal") event repeats many times-webrtc
After resignal, peers wont add to ur array and u'll got ur video stream from ... Error: cannot signal after peer is destroyed...
Read more >
Simple-peer NPM | npm.io
Destroy and cleanup this peer connection. If the optional err parameter is passed, then it will be emitted as an 'error' event on...
Read more >
PeerJS Documentation
Attempt to reconnect to the server with the peer's old ID. Only disconnected peers can be reconnected. Destroyed peers cannot be reconnected. If...
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