1

I'm new in node.js So the question can be quite naive.

I want to use node.js as a proxy between the javascript client and a windows program which has a API working through a defined port. So browser sends HTTP request to node.js. Node.js opens connection with the windows program, sends request, get a respons and returns the response to the javascript that called node.js ( AJAX )

Actually it has been realized so and works. The problem is that windows program wants to work persistent. So once the connection is opened it should stay alive.

And my node.js script opens connection. And the next call to node.js try to open connection again. And that leads to error.

So the question - what is the right way and middles to reuse the TCP connection in node.js. So that next call won't open new connection but go on with already opened one.

1 Answer 1

1

It is possible, but requires some coding on your side.

If it is not sticky (the same HTTP client does not require to use the same TCP connection):

  • Open an connection pools of TCP connections to the windows program
  • Listen to HTTP requests
  • If a HTTP request arrives find an unused TCP connection (if none is available, make a new one or wait)
  • Query the windows program, return the results to who ever called the HTTP request
  • Mark the TCP connection as free

If it is sticky (always use the same TCP connection for the same HTTP client):

  • If a HTTP clients connect and he hasn't one, give him session.
  • If not available for the session: create a TCP connection
  • Make the request, return the result
  • Store the TCP "connection" somewhere where you could reach it when the next request comes in and you could identify it by the HTTP clients session (Maybe make a timeout for clearing up)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Getting closer. :) Now is the question How node.js can "Store the TCP "connection" and reopen it within another call.
Put in a variable somewhere where it does not get garbage collected. Node.js is single threaded so you shouldn't into issues there as long as you do not use child processes.
Thanks, it works. I didn't even expect that it can be so easy :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.