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.

on("Signal") called twice

See original GitHub issue

I’m trying to make a connection between two peers and after there’s a connection i want to add their streams. This is the main part of the code:

add a stream:

getMedia(peer) {
    // Get media of current client
    navigator.mediaDevices
      .getUserMedia({ video: false, audio: true })
      .then((stream) => {
        peer.addStream(stream);
      })
      .catch(() => {});
}

callPeer method:

callPeer(peer, userToCall) {
    peer.on("signal", (data) => {
      this.socket.emit("call:offer", {
        receiverId: userToCall,
        signalData: data,
      });
    });

    peer.on("connect", () => {
      this.getMedia(peer);
      console.log("peers connected");
    });

    peer.on("stream", (stream) => {
      var audio = document.getElementById(userToCall);

      audio.srcObject = stream;
      audio.onloadedmetadata = function (e) {
        audio.play();
      };
    });
 }

acceptCall method:

  acceptCall(peer, callData) {
    peer.on("signal", (data) => {
      this.socket.emit("call:accept", {
        signalData: data,
        receiverId: callData.senderId,
      });
    });

    peer.on("stream", (stream) => {
      var audio = document.getElementById(callData.senderId);

      audio.srcObject = stream;
      audio.onloadedmetadata = function (e) {
        audio.play();
      };
    });

    peer.on("connect", () => {
      this.getMedia(peer);
      console.log("peers connected");
    });

    peer.signal(callData.signalData);
  }

The main problem is in acceptCall method in this line: peer.signal(callData.signalData); For some reason after i call this line, the peer.on("signal")(acceptCall method) is being called twice everytime i’m trying to make a call.

Also, it happens in two scenarios, because of different reasons(from what i understand):

The first time it happens is the initial call, i investigated a bit, and turns out it was related to renegotiation. so just adding a hacky if(!data.renegotiate) inside the peer.on("signal")(acceptCall method) solved it. like this:

 peer.on("signal", (data) => {
      if (!data.renegotiate) {
      this.socket.emit("call:accept", {
        signalData: data,
        receiverId: callData.senderId,
      });
     }
    });

The second time happens again on the same line, when it tries to re perform the call signaling after addStream called, and i have no idea why it happens in this scenario. but because of this, it tries to accept the call again(this.socket.emit("call:accept"...)) which causes errors.

not sure what to do here, tried to add logs to the library index.js, but can’t find the place that calls the on("signal") again.

thanks.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:24

github_iconTop GitHub Comments

1reaction
ranbe1commented, Jul 11, 2020

anyway, i figured out what was the problem. everytime i called acceptCall it redefined the .on("signal'), that’s why when simple-peer called emit it was being called twice. thanks for the help 😃

1reaction
nazar-pccommented, Jul 10, 2020

signal is expected to be called multiple times unless you specify trickle: false in peer configuration. This mechanism is called Trickle ICE and is designed to make connection establishment faster.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django post save signal getting called twice despite uid
It seems that the session object and LogEntry object were being saved as well creating multiple signals despite setting the dispatch_uid. What worked...
Read more >
Why would Signal method be called twice ? - Godot Engine
Hello,. I have a CanvasLayer/Control/Texture Button, where on pressed button I emit_signal. and print returns that the signal was emitted once.
Read more >
Troubleshooting Calling - Signal Support
You and your contact both have to be connected to the internet for your call to start ringing. It is likely that your...
Read more >
post_save signal getting called twice ! - Google Groups
Hi I am using django version 1.4.3. I am using two signals in my models.py one is m2m_changed, so i can have a...
Read more >
Order of execution if a slot is called twice by the same signal
I have the following situation: My onReadyRead() slot is reading from a TCP socket, collects the payload of TCP-packets until one packet on ......
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