Way to Non-Keep-Alive Connections with Node? - node.js

I'm trying to build a demo distributed app that shows how to load balance between many different containers. The app works fine, but I'm trying to force refreshing between different containers, and Chrome/Firefox are keeping the connection open. I thought that forcing the connection to close with .close() would work:
https://nodejs.org/api/http.html#http_request_end_data_encoding_callback
But this causes the server to close. Is there a way to tell Chrome/Firefox not to hold open the connection to a server in Node without closing the server?

Excellent question. As far as I know, there is no normal direct way to disable HTTP keep-alive with the built-in HTTP server. You might be able to work around this. The request object has a connection property which will contain the underlying socket. You could save a reference to the connection and close it after you have called end() on the response. This really isn't a great idea however. The client can use pipelining, sending multiple requests before the response from a previous one has finished.
The best thing to do is just add this to your response headers:
Connection: close
This will signal the client not to keep the connection open.

Related

Reusing RabbitMQ connection per expressjs api request

We are trying to use rabbitmq using this library called amqplib (not node-amqplib). Now all docs and examples I read say the connections should be long term. So my original thought of as soo nas someone makes a request to our web server (express), it will open a connection thus open a channel thus a queue so on and so forth. But that means per request we will be constantly opening and closing which is said ia not how you should ot it.
So, with that said, if you have an express server or some nodejs arch which involves pusblishing data per http request, is there some standard? do i make the connection object global and then any functions i need to use it, i pass it in/call it? is there som,e better way to get that connection instance per request without re-creating it?
Thanks in advance!

I am getting connection reset error when trying execute two queries at the same time in arangodb?

I am using arangojs v6.14.1 and arangodb version 3.6.4.. I also have the nodejs express app which is intended to serve client requests.
I am experiencing an issue while executing concurrent requests. The database connection hangup when I concurrently process client requests:
What the app will do when it receives a request?
Open a database connection -
db = new Database(dbConfig.url);
db.useDatabase(dbConfig.name);
db.useBasicAuth(dbConfig.username, dbConfig.password);
There are multiple middleware functions that need to access to perform various functions and accessibility checks. And for each middleware I tried to
open a new database connection ->
perform the action ->
close the connection ->
return the result to next middleware.
This works fine with single request at a time. But if I tried to call two APIs at same time, I am getting CONNECTIONRESET error. And also throwing socket hangup error.
I tried to commend out the close connection method and it started working fine for a while. But when I increased the number of connections, it again showing the same error as "CONNECTIONRESET".
I have searched the documentation of arangojs regarding the connection manipulation. But I haven't found any information regarding the same.
Any help would be deeply appreciated.
This might be too late for you, but I had a similar issue. After talking with ArangoDB support, it turned out to be an issue with sockets.
Looking at the output from netstat -aplnt I was seeing a lot of CLOSE_WAIT and TIME_WAIT messages, indicating that there were A LOT of connections that were not being closed properly.
The solution was to enable persistent connections (see arangojs config for keep-alive and maxSockets) and then re-using a single connection whenever possible. This was more than just a settings change, requiring some code modifications as well.

How to debug websocket client losing connection and reestablishing it

I'm using node.js, websockets/ws.
In my site sometimes a random client loses connection without losing connection to other internet stuff. Less then a second later they connect back, with a new socket. (There is a code that calls socket.onClose, which tries to reconnect back to server)
On the server side I can't see or log anything wrong. Everything looks like a normal disconnect, same as closing the browser tab.
I am guessing the reason is either socket related or client related but I don't know where to begin to debug this problem.
I got ping/pong responses with 60 second timer, this isn't it. The user usually loses connection while active.
How can I debug this problem and find the reason?
I keep all the session info, data, within the socket and that is why I do not want people to lose their connection.
Thanks

socket.io disconnects clients when idle

I have a production app that uses socket.io (node.js back-end)to distribute messages to all the logged in clients. Many of my users are experiencing disconnections from the socket.io server. The normal use case for a client is to keep the web app open the entire working day. Most of the time on the app in a work day time is spent idle, but the app is still open - until the socket.io connection is lost and then the app kicks them out.
Is there any way I can make the connection more reliable so my users are not constantly losing their connection to the socket.io server?
It appears that all we can do here is give you some debugging advice so that you might learn more about what is causing the problem. So, here's a list of things to look into.
Make sure that socket.io is configured for automatic reconnect. In the latest versions of socket.io, auto-reconnect defaults to on, but you may need to verify that no piece of code is turning it off.
Make sure the client is not going to sleep such that all network connections will become inactive get disconnected.
In a working client (before it has disconnected), use the Chrome debugger, Network tab, webSockets sub-tab to verify that you can see regular ping messages going between client and server. You will have to open the debug window, get to the network tab and then refresh your web page with that debug window open to start to see the network activity. You should see a funky looking URL that has ?EIO=3&transport=websocket&sid=xxxxxxxxxxxx in it. Click on that. Then click on the "Frames" sub-tag. At that point, you can watch individual websocket packets being sent. You should see tiny packets with length 1 every once in a while (these are the ping and pong keep-alive packets). There's a sample screen shot below that shows what you're looking for. If you aren't seeing these keep-alive packets, then you need to resolve why they aren't there (likely some socket.io configuration or version issue).
Since you mentioned that you can reproduce the situation, one thing you want to know is how is the socket getting closed (client-end initiated or server-end initiated). One way to gather info on this is to install a network analyzer on your client so you can literally watch every packet that goes over the network to/from your client. There are many different analyzers and many are free. I personally have used Fiddler, but I regularly hear people talking about WireShark. What you want to see is exactly what happens on the network when the client loses its connection. Does the client decide to send a close socket packet? Does the client receive a close socket packet from someone? What happens on the network at the time the connection is lost.
webSocket network view in Chrome Debugger
The most likely cause is one end closing a WebSocket due to inactivity. This is commonly done by load balancers, but there may be other culprits. The fix for this is to simply send a message every so often (I use 30 seconds, but depending on the issue you may be able to go higher) to every client. This will prevent it from appearing to be inactive and thus getting closed.

node.js event streaming as a client

I have a net stream that I want to use, however I can't use it directly due to the CORS limitation and that EventSource doesn't support authentication ..
I want to use node.js to get the stream from the source (domainA) and relay it to the requester.
so the flow would be: domainA --> node.js --> client
I was using https.request to get the data, but the thing is that its closing the connection after the first time.
so my question is - how would I proper implement this on node.js? how do I keep the connection alive and the data coming?
I was actually able to resolve this myself. what I was trying to do is simply 'mirror' an event source due to CORS restrictions. the connection kept closing after the first request, I found out that I was actually sending the wrong headers. After sending "Accept": "text/event-stream" the connection was kept opened and the data flowing. Thanks anyway!

Resources