I'm using superagent
and while inspecting the network I noticed that superagent is creating a new TCP connection for each request. I'm using superagent for a sequence of requests which results in a large number of TCP connections (can reach several hundreds).
I tried to follow this idea and use agentkeepalive
package however this approach has some downsides:
- While superagent works out-of-the-box with http and https,
agentkeepalive
needs to be defined per protocol. - As a result of the previous section, in case I do an http request that redirects to https request I get an error of
Protocol "https:" not supported. Expected "http:"
since the protocol has changed. - As a result of section 2 the app crushes since with an
uncaughtException
(which happens only upon redirects and not when using the wrong protocol i.e. the httpagentkeepalive
for https request)
Needless to say that using request.set('Connection', 'keep-alive');
didn't solve it.
So my question is: how can I reuse the TCP connection while using superagent
without getting errors in redirects? Is there another solution to reuse the TCP connection in superagent
beside the agentkeepalive
package?