I'm trying to call REST API asynchronously in a for loop using XMLHttpRequest module. I'm making 400 requests in 1 loop, with a wait time of 1 sec after every 100 requests. This works fine on clientside JavaScript. However, when I run on NodeJS using the same module (it's not natively available, I had to download from npmjs) , I'm getting this error after about 230 requests. Any idea if there is another module that I can use to better handle this bulk API requests?
Error: connect ETIMEDOUT at TCPConnectWrap.afterConnect [as oncomplete]
I finally found the solution to my problem.
When using request module for calling REST APIs , you need to specify pool variable in the options. This serves as maxsockets, which is a declaration for concurrent request processing.
Sample code is posted below for those who run unto this issue:
For more info, check out below post: How to use Request js (Node js Module) pools
var separateReqPool = {maxSockets: 20};
var request = require('request');
var url_array = ['url1','url2','url3'];//Array of all the urls to call
async.map(url_array, function(item, callback){
request({url: item, pool: separateReqPool}, function (error, response, body) {
//Do Something with the response
});
}, function(err, results){
console.log(results);
});
}).
catch((err) => {
console.log(err);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With