establish several http connections without closing

37 views
Skip to first unread message

Nuno Barros

unread,
May 21, 2014, 12:06:09 PM5/21/14
to nod...@googlegroups.com
hello, I'am in a project where i need to establish the most possible http connections and keep them open, using node.js but I'm not sure how I could do it. if anyone can put me in the right way it would be great.

thanks in advance

Aria Stewart

unread,
May 21, 2014, 2:39:39 PM5/21/14
to nod...@googlegroups.com

On May 21, 02014, at 12:06, Nuno Barros <legali...@gmail.com> wrote:

> hello, I'am in a project where i need to establish the most possible http connections and keep them open, using node.js but I'm not sure how I could do it. if anyone can put me in the right way it would be great.
>
> thanks in advance

In node 0.10, disable the http agent; in node 0.11+, that shouldn’t be needed. (Someone correct me if I’m wrong there!)

Then: use the http library and start a bunch of connections. If you start sending and don’t end, they’ll stay open.

Do you need them to stay open in inter-request keepalive state, or just occupy sockets?

Aria
signature.asc

Nuno Barros

unread,
May 22, 2014, 4:33:13 AM5/22/14
to nod...@googlegroups.com
yes i need them to stay keepalive state, my objective is to create 100 http conectins if no error is given + 100 till i can check a max of nat ports in a router

Nuno Barros

unread,
May 22, 2014, 5:53:26 AM5/22/14
to nod...@googlegroups.com

in the moment i got this, not really sure if its keeping the connection open and the node i'am using is v0.10.28

var http = require('http');
 
var http_options = {
  hostname: '193.136.212.161',
  port: 80,
  path: '/',
  method: 'GET',
  agent: false,
  headers: {
      'Connection':'keep-alive'
    }
};
 
http.request(http_options, function(res) {
}).on("socket", function (socket) {
    socket.on('connect', function(res, socket, head) {
            console.log('got connected!');
    });
});

Aria Stewart

unread,
May 22, 2014, 11:58:13 AM5/22/14
to nod...@googlegroups.com

On May 22, 02014, at 4:33, Nuno Barros <legali...@gmail.com> wrote:

> yes i need them to stay keepalive state, my objective is to create 100 http conectins if no error is given + 100 till i can check a max of nat ports in a router

That doesn’t sound like you need keepalive state, just open sockets.
That’s probably not what you want. You don’t want to use GET (No body, whole request is sent and then socket can be closed)

try something like so:

http_options.method = ‘POST’;
var req = http.request({method: ‘POST’, path: ‘/‘, agent: false, hostname: ‘193.136.212.161’}, function (res) {});
setTimeout(function () { req.end(‘.’); }, 10000);

That should start the request right away but not complete it for ten seconds.


signature.asc
Reply all
Reply to author
Forward
0 new messages