Ibrahim Almahfooz
Ibrahim Almahfooz

Reputation: 317

How to enable HTTP keep-alive in the 'http request' node in Node-RED?

On Node-RED I'm setting up a front-end that interfaces with an API backend however I can't use the API due to http "keep-alive" is not possible on Node-RED "http request" node.

I'm designing a front-end on Node-RED dashboard which interfaces with a soap API backend that at first it requires an API login in order perform subsequent API requests. The backed-end expect me to first send the credentials to the url http://10.10.10.10:8888/, then it reply with a new URL in the locaiton header (sample response http://10.10.10.10:8888/lskdh234hv02n), this new URL I must use in the next API requets.

In order to use the newly provided URL by the response, the first HTTP request should keep the initial HTTP request open by using keep-alive property. However this is not happening when sending HTTP request from the "http request" node on Node-RED. I confirmed this using wireshark and compared the capture with another capture using postman when doing the same API activity.

When I use postman, it works like charm and you could tell there is a keep-alive on http going on.

On Node-RED, I tried to inject the header "connection: keep-alive" in the before the http request node, however Node-Red is overwriting this header value with "connection: close\r\n" specifically. I confirmed this behavior by sending other headers like User-Agent, Test-Header, I can see them in my request and they arrive to the backend but the connection header is still coming with "close\r\n"

Here is what I'm injecting before the http request node to manipulate the connection header:

msg.headers = {
"connection" : "keep-alive",
"my-custom-header": "samplevalue",
"User-Agent": "My User Agent"
}
return msg; 

I see "User-Agent" and "my-custom-header" correctly set but the "connection" is still close.

Now I start to think that Node-RED http requets node is not allowing http connection reuse (persistant http connection) and I'm wondering whether there is a workaround on Node-RED to allow it or some settings on Node-RED itself to make it capable of maintaining http persistent connection?

EDIT: Actually regardless the connection header is set to keep-alive or close, I always see the FIN flag in the TCP connection initiated by Node-RED (not the server). It seems like Node-RED is not maintaining HTTP connections with keep-alive header.

Appreciate your help

Upvotes: 2

Views: 8075

Answers (2)

Ibrahim Almahfooz
Ibrahim Almahfooz

Reputation: 317

Managed to enable the HTTP persistency which got the issue fixed. Simply I used the www-request node (an alternative enhanced node similar to the core http-request node) and enabled the forever feature on the www-request.

Upvotes: 1

hardillb
hardillb

Reputation: 59618

The http-request node isn't currently really set up for a single persistent connection. The current implementation is intended to make a single request for each incoming message and then output a single response.

What you are describing is probably a valid feature request that will be best discussed on the Node-RED forum and if you want to look at adding a mode to the node to re-use a single connection then a pull request would probably be looked at favourably (please discus on the forum first and don't open an issue).

A quick look at the request npm module docs implies that setting the forever option might help.

Upvotes: 1

Related Questions