Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if net.Socket connection dies - node.js

Background

I am communicating with a machine using net.Socket via TCP/IP.

I am able to establish the connection and to send and receive packets of Buffers, which is all fine.

Problem

The problem is that if I manually disconnect the internet cable from the machine, my Node.js connection doesn't fire the close event and I have no means to know if something failed!

Code

let socket;

const start = async function ( config ) {

    await connecToBarrier( config )
        .then( () => console.log( "connected to barrrier" ) )
        .catch( err => {
            throw new Error( `Cannot connect to barrier: ${err}` );
        } );
};

const connecToBarrier = function ( connectOpts ) {
    return new Promise( ( resolve, reject ) => {
        //connectOpts is an object with 'port' and 'host'
        socket = net.createConnection( connectOpts, () => {
            //once I receive a 'hello world' from the machine, I can continue
            socket.once( "data", () => resolve() );
        } );

        socket.on( "connection", () => {
            console.log("onConnection says we have someone!");
        } );

        socket.on( "error", err => {
            console.log(`onError says: ${err}`);
            reject(err);
        } );

        socket.on( "timeout", () => {
            console.log("onTimeout says nothing");
            reject();
        } );

        socket.on( "end", () => {
            console.log("onEnd says nothing");
            reject();
        } );

        socket.on( "close", err => {
            console.log(`onClose says: ${err}`);
            reject(err);
        } );
    } );
};


start();

Research

@robertklep mentioned the setKeepAlive option, however according to

How to test socket.setKeepAlive in NodeJS

it doesn't work. A more profound research shows that this is highly dependant on the Operative System you are using, as per https://stackoverflow.com/a/18678494/1337392

So in other words, unless I am willing to wait several minutes for my heartbeats to actually do something, I don't see a way out of this.

Question

How do I detect if a connection died?

like image 609
Flame_Phoenix Avatar asked May 30 '17 11:05

Flame_Phoenix


People also ask

How do you check Socket is connected or not?

If you need to determine the current state of the connection, make a nonblocking, zero-byte Send call. If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected.

How do you check Socket is connected or not Nodejs?

You can check the socket. connected property: var socket = io. connect(); console.

What is allowHalfOpen?

What is allowHalfOpen? allowHalfOpen {boolean} Indicates whether half-opened TCP connections are allowed. See net. createServer() and the 'end' event for details. Default: false .

How many Socket connections can node js handle?

The theoretical limit is 65k connections per IP address but the actual limit is often more like 20k, so we use multiple addresses to connect 20k to each (50 * 20k = 1 mil).


1 Answers

I had the same issue. I ended up doing this as a fix:

const _connect = () => {
  const socket = new net.Socket();
    socket.connect(this.port, this.host);
    socket.setTimeout(10000);

    socket.on('timeout', () => {
        socket.destroy();
        _connect(); 
    });
}
like image 116
Amit Wagner Avatar answered Sep 28 '22 09:09

Amit Wagner