Node.JS net module handling unexpected connection loss
<p>I can't figure out one problem I got.
I'm using the Net module on my Node.JS server which is used to listen to client connections.</p>

<p>The client do connect to the server correctly and the connection remains available to read/write data. So far, so good. But when the client unexpectedly disconnects (ed. when internet falls away at client side) I want to fire an event server side.</p>

<p>In socket.io it would be done with the 'disconnect' event, but this event doesn't seem to exist for the Net module. How is it possible to do?</p>

<p>I've searched on Google/StackOverflow and in the Net documentation (<a href="https://nodejs.org/api/net.html" rel="nofollow">https://nodejs.org/api/net.html</a>) but I couldn't find anything usefull. I'm sry if I did mis something.</p>

<p>Here is a code snippet I got: </p>

<pre><code>var net = require('net');

var server = net.createServer(function(connection) {
    console.log('client connected');
    connection.wildcard = false;//Connection must be initialised with a configuration stored in the database
    connection.bidirectional = true;//When piped this connection will be configured as bidirectional

    connection.setKeepAlive(true, 500);
    connection.setTimeout(3000);

    connection.on('close', function (){
        console.log('Socket is closed');
     });

    connection.on('error', function (err) {
        console.log('An error happened in connection' + err.stack);
    });
    connection.on('end', function () {
        console.log('Socket did disconnect');
    });
    connection.on('timeout', function () {
        console.log('Socket did timeout');
        connection.end();
    });
    connection.on('data', function (data) {
        //Handling incoming data
    });

});

serverUmrs.listen(40000, function () {
    console.log('server is listening');
});
</code></pre>

<p>All the events(close, end, error, timeout) don't fire when I disconnect the client(by pulling out the UTP cable).</p>

<p>Thanks in advance!</p>

<p><strong>EDIT:</strong>
I did add a timeout event in the code here above but the only thing that happens is that the socket does timeout after 3 seconds everytime the client does connect again. Isn't KeepAlive enough to make the socket not Idle? How is it possible to make the socket not idle without to much overhead. It may be possible that there are more than 10,000 connections at the same time which must remain alive as long as they are connected (ie respond to the keepalive message).</p>