1

In case the link between a node.js server and a client gets broken, and the disconnection event has not been triggered by a timeout yet, is there any way to detect if a particular event emitted on the client-side could not be delivered to the server side by Socket.IO?

1 Answer 1

1

I had a deeper look into this, as I am not an expert on socket.io or node.js.

  1. which type of socket?

sockets.io abstracts which method it uses to connect to the server, depending on the browser capabilities. The default is to use WebSockets (HTML5).

sockets.io on the server side however attaches to the webserver of node.js, and so it will always use TCP, since that's what HTTP runs on. So whichever method the client is using it will always use TCP underneath. So you don't need to worry about UDP sockets.

  1. is socket.io emitting errors?

TCP theoretically checks that each packet arrives, and so TCP should bubble up connection errors to the socket.io library. So you might try something like:

<script>
  var socket = io.connect('http://localhost/');

  socket.on('error', function (message) { 
    alert( 'error in transport: ' + message );
  });

BTW, I haven't tried this myself, but I read the code of socket.io.js (which you should find through your browser as this is dynamically created by socket.io depending on the config of transports, it seems to me) saw which events are emitted using publish(). An error should also trigger a disconnect, reconnect sequence, but the 'close' doesn't fire till its absolutely closed (it looks to me) which might take some time. But it looks like the error should fire immediately.

  1. Otherwise I have two ideas:
    a. use node.js socket.setTimeOut to set a shorter timeout, and see what happens, b. or try force the error on the socket by some means, i.e. in between reads in your read loop (which you must have since you are presumably using a non-blocking socket), try write into the socket.
Sign up to request clarification or add additional context in comments.

6 Comments

How do I ensure that Socket.IO always uses net.Socket() ?
Alright ... I'll go through the socket.io.js code and tell you what I come up with.
OK ... so I spent a few hours trying to understand how things were worked ... and your answer seems perfectly reasonable. But apparently both of us missed something, because when I tried out something along the lines of the code you pasted above, things didn't work. I examined the Socket object using the Chrome JavaScript Console, pastebin.com/PnsNVkqM, and discovered that events seemed to be divided into 2 classes ... internal ones and external ones.
I've waited for a long time hoping that my function for connect_failed will be called, but nothing happened, and I've already exceeded maxAttempts.
The problem is in the Operating System TCP stack, which will behave differently depending on your OS and how the connection was interrupted. Are you aware of this - github.com/LearnBoost/socket.io/issues/844 - I have added some more suggestion to my post.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.