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.

connect() sometimes fails to fire error event when failing to connect

See original GitHub issue

Attempt to connect to a peer that doesn’t exist, and listen for the error event. Expected: should fire the error event. Observed: sometimes it does, sometimes NOTHING. It fails to connect (which is expected), but it does not fire the error event either. No open, close or error event, nothing.

Example code:

    var peer;
    var conn;
    var otherId="whatever";
    var connected=false;

    $(function(){
      peer=new Peer({ 
        host: 'example.com',
        port: 9000,
        key: 'xxx'
      });

      startConnect();
    });

    function startConnect() {
        connected=false;
        $("#connection-info").html("Connecting...");
        conn=peer.connect(otherId);
        conn.on('open', function(){
            console.log("connection open");
            initDataConnection();
        })
        conn.on('close', function(){
            console.log("Connection closed");
            connected=false;
            startConnect();
        });
        conn.on('error', function() {
            connected=false;
            $("#connection-info").html("Connection error.");
            console.log("connection error ");
        });
    }

    function initDataConnection() {
      connected=true;
      conn.on('data', function(data){
        console.log("Received: "+data);
      });

    }

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
teo1978commented, Apr 24, 2016

Sorry for the previous deleted comments. I had some errors in my test code.

Are there any news? I still confirm the issue and I can’t find a precise pattern. This bug is VERY SERIOUS, as not being able to reliably detect when a connection cannot be established or is lost renders peerjs totally unusable.

Here’s a recap:

  • I have a Peer object that tries to connect to a remote peer via connect()
  • I listen to the error event on both the Peer object itself and the DataConnection object returned by connect. I also listen to the close event on the DataConnection object.
  • usually, as expected, if I try to connect to a remote Peer that can’t be reached (e.g. it doesn’t exist), an error event is fired by the Peer object
  • usually, as expected, if the remote Peer goes away, a close event is fired either by the DataConnection object.
  • However, at random times, I call connect(), and neither the open nor the error event are fired.

The way I systematically reproduce the issue is as follows:

  • Whenever I detect an error event either in the Peer or the DataConnection object, or a close event on the DataConnection, I set a timeout and start all over, creating a new Peer and trying to connect it to the remote, whose id is known.
  • I start with the remote peer listening. So the connection is succesful at the first try. The connection is established, and the open event is fired as expected
  • Then I take the remote down. So, my Peer detects the close event as expected and starts retrying.
  • it keeps detecting the error event as expected and keeps retrying
  • then I restart the remote which start listening again

At this point, it would be expected that the connection from the local Peer succeeds. Instead, it neither succeeds neither triggers an error. Neither the open event (on DataConnection) nor the error event (on the Peer) are fired.

0reactions
afrokickcommented, Sep 12, 2019

Should be fixed in 1.0.4+

Read more comments on GitHub >

github_iconTop Results From Across the Web

Socket.io Client Fire Event if Can't connect to server
I'm running a simple node.js server on Amazon EC2 that is running socket.io for me. I'm working on a chrome extension that sends...
Read more >
Troubleshooting connection issues | Socket.IO
You are trying to reach a plain WebSocket server; The server is not reachable; The client is not compatible with the version of...
Read more >
Troubleshoot common issues with triggers - Power Automate
This error means that Power Automate tried multiple times to establish a connection to register the trigger and failed.
Read more >
Let It Crash: Best Practices for Handling Node.js Errors on ...
Let's say we have a server running. It's receiving requests and establishing connections with clients. But what happens if the process crashes?
Read more >
Node.js Error Handling Best Practices: Ship With Confidence
throw new Error('database failed to connect');. That may sound pretty overwhelming. For simplicity, let's break it down. Two distinct things are ...
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