node.jsreactjssimple-peer

How to fix signal after peer is destroyed?


I am creating a video chat app, but when second person connected, server return some strange errors. Please help me to solve this error.

Actual problem comes when I open new tab and paste same URL for testing.

I am using "simple-peer": "^9.7.2",

Error: cannot signal after peer is destroyed

Here is the screenshot too

enter image description here

Server Code

  socket.on("returning-signal", (payload) => {
    io.to(payload.callerID).emit("receiving-returned-signal", {
      signal: payload.signal,
      id: socket.id,
    });
  });

Client Side

 socketRef.current = io.connect("http://localhost:1234");
    navigator.mediaDevices
      .getUserMedia({ video: videoConstraints, audio: false })
      .then((stream) => {
        userVideo.current.srcObject = stream;
        socketRef.current.emit("join-room", roomID);
        socketRef.current.on("all-users", (users) => {
          const peers = [];
          users.forEach((userID) => {
            const peer = createPeer(userID, socketRef.current.id, stream);
            peersRef.current.push({
              peerID: userID,
              peer,
            });
            peers.push(peer);
          });
          setPeers(peers);
        });

        socketRef.current.on("user-joined", (payload) => {
          const peer = addPeer(payload.signal, payload.callerID, stream);
          peersRef.current.push({
            peerID: payload.callerID,
            peer,
          });

          setPeers((users) => [...users, peer]);
        });

        socketRef.current.on("receiving-returned-signal", (payload) => {
          const item = peersRef.current[0];
          item.peer.signal(payload.signal);
        });
      });

  function createPeer(userToSignal, callerID, stream) {
    const peer = new Peer({
      initiator: true,
      trickle: false,
      stream,
    });

    peer.on("signal", (signal) => {
      socketRef.current.emit("sending-signal", {
        userToSignal,
        callerID,
        signal,
      });
    });

    return peer;
  }


Solution

  • Solution found

    Problem is in this version "simple-peer": "^9.7.2", i changed the version to 9.6.2 and it works like charm. ✨🤩