11

I'm writing a node.js application that needs to talk to a server. It establishes an http connection with the following code:

var client = http.createClient(u.port, u.hostname, u.secure);
client.on("error", function(exception) {
    logger.error("error from client");
});
var request = client.request(method, u.path, headers);

I don't see any option in the node.js documentation for setting a timeout on the connection, and it seems to be set to 20 seconds by default. The problem I'm having is that I have users in China on what appears to be a slow or flaky network, who sometimes hit the timeout connecting to our datacenter in the US. I'd like to increase the timeout to 1 minute, to see if that fixes it for them.

Is there a way to do that in node.js?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Dan List
  • 113
  • 1
  • 1
  • 5
  • 1
    The correct answers are all in this duplicate question: http://stackoverflow.com/questions/6214902/how-to-set-a-timeout-on-a-http-request-in-node (especially see douwe's answer) – Sandman4 Mar 05 '13 at 10:51
  • Possible duplicate of [How to set a timeout on a http.request() in Node?](https://stackoverflow.com/questions/6214902/how-to-set-a-timeout-on-a-http-request-in-node) – Andrew Marshall Jan 02 '18 at 22:09

4 Answers4

8

Try

request.socket.setTimeout(60000); // 60 sec

IvanM
  • 2,913
  • 2
  • 30
  • 30
Matjaz Lipus
  • 702
  • 1
  • 5
  • 10
4

I think you can do something like:

request.connection.setTimeout(60000)

request.connection returns the net.Stream object associated with the connection. and net.Stream has a setTimeout method.

Toby Hede
  • 36,755
  • 28
  • 133
  • 162
  • Doesn't work. I also found there's a client.setTimeout() method, that doesn't work either. I think these are timeouts for traffic on the connection, not for establishing the connection. – Dan List Aug 30 '10 at 23:08
  • Right. Ok then ... I will have another trawl through some code. Might be worth checking how/if some of the node frameworks handle this. – Toby Hede Aug 31 '10 at 04:09
  • Thanks that solved a WebSocket proxy problem for me ;) The connections were dead after a minute or so. – Van Coding Apr 21 '11 at 08:27
3

There is no capability in Node to increase connect timeout. Since usually connect timeout (i.e. connection establishing timeout) is OS-wide setting for all applications (e.g., 21 seconds in Windows, from 20 to 120 seconds in Linux). See also Timouts in Request package.

In contrast, Node allows to set decreased timeout and abort connecting even in case when the connection is not yet established.

The further timeouts (in case of connection has been established) can be controlled according to the documentation (see request.setTimeout, socket.setTimeout).

ruvim
  • 7,151
  • 2
  • 27
  • 36
2

You have to wait for the client socket connection to be established first, before setting the timeout. To do this, add a callback for the 'socket' event:

req.on('socket', function (socket) {
    myTimeout = 500; // millis
    socket.setTimeout(myTimeout);  
    socket.on('timeout', function() {
        console.log("Timeout, aborting request")
        req.abort();
    });
}).on('error', function(e) {
    console.log("Got error: " + e.message);
    // error callback will receive a "socket hang up" on timeout
});

See this answer.

Community
  • 1
  • 1
Pavel
  • 3,481
  • 26
  • 31