I am trying to set-up a HTTP client to keep the underlying connection open (keep-alive) in node.js, but it seems that the behaviour does not correspond to the docs (http://nodejs.org/api/http.html#http_class_http_agent).
I am creating a new HTTP agent, setting the maxSockets property to 1 and requesting an url (for instance http://www.twilio.com/
) every second.
It seems that on every request the socket is closed and a new socket is created. I have tested this with node.js 0.10.25 and 0.10.36 under Ubuntu 14.04.
Has anyone been able to get keep alive to work?
Here is the code:
var http = require("http");
var agent = new http.Agent();
agent.maxSockets = 1;
var sockets = [];
function request(hostname, path, callback) {
var options = {
hostname: hostname,
path: path,
agent: agent,
headers: {"Connection": "keep-alive"}
};
var req = http.get(options, function(res) {
res.setEncoding('utf8');
var body = "";
res.on('data', function (chunk) {
body += chunk;
});
res.on('end', function () {
callback(null, res, body);
});
});
req.on('error', function(e) {
return callback(error);
});
req.on("socket", function (socket) {
if (sockets.indexOf(socket) === -1) {
console.log("new socket created");
sockets.push(socket);
socket.on("close", function() {
console.log("socket has been closed");
});
}
});
}
function run() {
request('www.twilio.com', '/', function (error, res, body) {
setTimeout(run, 1000);
});
}
run();