3

I'm trying to use Socket.io (with NodeJS and AngularJS) to detect when a client disconnets due to a connection drop.

I've tried catching socket.on

  • disconnect
  • connect_error
  • reconnect_attempt
  • reconnecting

but it looks like they aren't fired client-side. What am I doing wrong?

Since the connection drop is a client-side issue I expect I should be able to detect it on the client but maybe I'm wrong.

Could someone please help me? Thank you!


**EDIT:** This is the code I'm using
socket.io.timeout(1000);
console.log(socket);
socket.on("open", function(){
    console.log("Connected");
});
socket.on("reconnect_attempt", function(){
    console.log("Attempt");
});
socket.on("reconnecting", function(){
    console.log("Reconnecting");
});
socket.on("disconnect", function(){
    $log.error("Client disconnected");
});

I'm faking the connection drop trough Chrome's device mode.

Luca De Nardi
  • 2,280
  • 16
  • 35
  • The doc says you should get some of those events client side. Can you show us your code and how are you triggering the connection drop? – jfriend00 Aug 27 '15 at 07:47
  • I updated the question – Luca De Nardi Aug 27 '15 at 07:52
  • I wanted to see what `socket` is. Where did you get that from? – jfriend00 Aug 27 '15 at 08:21
  • Oh sorry, in a module i have core.socket = io.connect(); and via a getSocket function I make it available to others – Luca De Nardi Aug 27 '15 at 08:33
  • And, how are you making the connection drop? – jfriend00 Aug 27 '15 at 08:37
  • Trough Chrome Console (Device Mode > Network > Offline) or by removing the network cable. As an update, I've been able to catch the events after 25 MINUTES since I disconnect the cable. It looks like the events are getting caught, now the problem is how to consider the client disconnected after just some seconds of inactivity.. – Luca De Nardi Aug 27 '15 at 08:50
  • You can implement your own ping/pong messages to the server. If you send a ping and don't get a pong back within a short period of time, then you must be disconnected. Since the socket.io library is already doing ping/pong under the covers, you might be able to register to see those messages (I'm not sure about that). You may have to pour over the socket.io/engine.io client code to see how these events are fired. I would be surprised if there are not some events for you to see that the client knows it has lost contact with the server. – jfriend00 Aug 27 '15 at 08:58
  • It looks like this isn't covered in Docs, that's why I'm hoping to find an answer here :) – Luca De Nardi Aug 27 '15 at 08:59
  • In my experience, there don't seem to be many folks here that know the answers to off-the-main-path things like this - I think you should be looking in the source code to see how it actually works. You can search for all these message names and see when they are triggered. The socket.io doc is quite superficial. I've spent lots of time in the source code getting my questions answered. – jfriend00 Aug 27 '15 at 09:01

1 Answers1

0

Here's the thing: there is no reliable way to detect whether a connection is dead or not. Especially under such an extreme events like random network failures. Welcome to the horrors of networking! This is because of the two generals' problem.

But we can approximate! Force each peer to send pings periodically, read pongs and setup timeouts. And emit your own "disconnected because no activity for X seconds" event. This can be done on both the client and the server side if necessary (although on the server side this is simpler: just periodically remove inactive sockets).

I've been able to catch the events after 25 MINUTES since I disconnect the cable

It seems that this is the internal TCP timeout you have on your test machine. Note that disconnecting a cable is not the same as dropping the connection. After all you can plug it back in and everything may work just fine, like nothing happened. This is important because you don't want to react to every sporadic (but irrelevant) networking issue there is.

Also you may want to read this: TCP Socket no connection timeout

freakish
  • 54,167
  • 9
  • 132
  • 169