Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

setting node's http.request timeout

setting node's http.request timeout

Problem

I'm testing a server and I need to keep alive some connection as long as possible. I would like to set by hand the Timeout of http.request() function. This is what I do in my code.

var req = http.request(options, function(res) {
        res.setEncoding('utf8');

        res.on('data', function (chunk) {
            //do something with chunk
        });
});

I tried something like req.on('socket' function(socket){ socket.setTimeout(SOCKET_TIMEOUT); }); but it gives me some strange errors and I think it's not the right way to do it. For your info the error I get using that approach is the following

Trace: 
    at Socket. (events.js:133:17)
    at ClientRequest. (/path/server_nologs.js:168:10)
    at ClientRequest.emit (events.js:88:20)
    at Array.1 (http.js:1250:9)
    at EventEmitter._tickCallback (node.js:192:40)
(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.

Which is kinda cryptic to me. Can someone explain me how to set the timeout? Otherwise, if my approach is correct, how can I avoid getting that error?

Problem courtesy of: Masiar

Solution

Here's what I'm using: (works for me)


req = httpClient.request(urlParts.method, urlParts.path, urlParts);
req.connection.setTimeout(HTTP_REQUEST_TIMEOUT);

where HTTP_REQUEST_TIMEOUT is a var I declared before set to 30000

Solution courtesy of: ControlAltDel

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

setting node's http.request timeout

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×