node.jssocketswebsockettcpeconnrefused

node.js - handling TCP socket error ECONNREFUSED


I'm using node.js with socket.io to give my web page access to character data served by a TCP socket. I'm quite new to node.js.

User ----> Web Page <--(socket.io)--> node.js <--(TCP)--> TCP Server

The code is mercifully brief:

io.on('connection', function (webSocket) {
    tcpConnection = net.connect(5558, 'localhost', function() {});

    tcpConnection.on('error', function(error) {
        webSocket.emit('error', error);
        tcpConnection.close();
    });

    tcpConnection.on('data', function(tcpData) {
        webSocket.emit('data', { data: String.fromCharCode.apply(null, new Uint8Array(tcpData))});
    });
});

It all works just fine in the normal case, but I can't guarantee that the TCP server will be there all the time. When it isn't, the TCP stack returns ECONNREFUSED to node.js - this is entirely expected and I need to handle it gracefully. Currently, I see:

events.js:72
    throw er; // Unhandled 'error' event
          ^
Error: connect ECONNREFUSED
    at errnoException (net.js:904:11)
    at Object.afterConnect [as oncomplete] (net.js:895:19)

... and the whole process ends.

I've done a lot of searching for solutions to this; most hits seem to be from programmers asking why ECONNREFUSED is received in the first place - and the advice is simply to make sure that the TCP server is available. No discussing of handling failure cases.

This post - Node.js connectListener still called on socket error - suggests adding a handler for the 'error' event as I've done in the code above. This is exactly how I would like it to work ... except it doesn't (for me), my program does not trap ECONNREFUSED.

I've tried to RTFM, and the node.js docs at http://nodejs.org/api/net.html#net_event_error_1 suggest that there is indeed an 'error' event - but give little clue how to use it.

Answers to other similar SO posts (such as Node.js Error: connect ECONNREFUSED ) advise a global uncaught exception handler, but this seems like a poor solution to me. This is not my program throwing an exception due to bad code, it's working fine - it's supposed to be handling external failures as it's designed to.

So

Oh, and:

$ node -v
v0.10.31

Solution

  • I ran the following code:

    var net = require('net');
    
    var client = net.connect(5558, 'localhost', function() {
      console.log("bla");
    });
    client.on('error', function(ex) {
      console.log("handled error");
      console.log(ex);
    });
    

    As I do not have 5558 open, the output was:

    $ node test.js
    handled error
    { [Error: connect ECONNREFUSED]
      code: 'ECONNREFUSED',
      errno: 'ECONNREFUSED',
      syscall: 'connect' }
    

    This proves that the error gets handled just fine... suggesting that the error is happening else-where.

    As discussed in another answer, the problem is actually this line:

    webSocket.emit('error', error);
    

    The 'error' event is special and needs to be handled somewhere (if it isn't, the process ends).

    Simply renaming the event to 'problem' or 'warning' results in the whole error object being transmitted back through the socket.io socket up to the web page:

    webSocket.emit('warning', error);