I was recently refactoring some code and came across this piece of code in server.js.
I looked out for the docs and I still have some unanswered questions.
const server = app.listen(port, function () {
console.log('Server started on port ' + port);
});
server.timeout = 600000 // 6 mins.
What is server.timeout actually doing above? If the response has to time out in 6 minutes, how is my download API still working? Each download takes more than 10 minutes to download. Also, I send a response back to the client after download is complete.
From the express docs:
The app.listen() method returns an http.Server object
From the Node.js docs:
The number of milliseconds of inactivity before a socket is presumed to have timed out.
emphasis added
This means that if there's an actively streaming download, this property will not apply. It only applies to sockets where ACKs are not received by the client for 6 minutes.
Related
Problem:
I am doubting that something in my network system is slow which is why responses from NodeJS server arrives late. I've noticed that the server is ready with the response fairly fast however, it takes time to reach the web browser.
Question:
What are the ways I can confirm whether this is infact happening or not? Basically to capture the time when the response left from my NodeJS server and then from the physical server and then compare it with the time that it arrived on the client web browser. How can I do this?
What I tried:
I tried putting a console.log after ctx.body to identify the point of time when the response left the server. But is this the correct point or can I go further down? I am a little unsure. Please advise.
FYI, I am using Koa.js.
As we cannot always assume that the time on the client is exactly the same as on the server, what you theoretically can do is:
client side: before sending a request to the server, get the current time with
const startClient = Date.now()
on the server side you implement a small request time (koa) middleware like this:
app.use(function *(next){
const startServer = new Date.now();
await next;
const ms = (new Date.now()) - startServer;
ctx.set('X-Server-Time', ms);
});
Place this middleware before defining your routes. This should return a time the server took to complete its task to the client (in the X-Server-Time header). So this is basically the time from when the server receives the request till he is ready and sends results back.
as soon as the client gets back any information from the server, get again the current time and calculate time spend overall:
myHeaders = response.headers;
if (myHeaders[X-Server-Time]) {
const msServer = parseInt(myHeaders[X-Server-Time]);
const endClient = Date.now();
const msOverall = endClient - startClient;
// output time consumed
console.log('Overall time in ms to complete : ' + msOverall);
console.log('Server time in ms to complete : ' + msServer);
console.log('Network time in ms to complete : ' + msOverall - msServer);
}
Code not testet, but I hope this gives an idea how to measure timing...
Try Koa.js in debug mode and add a logger middleware to identify server side issues.
Use the network inspector in Chrome or any equivalent in other browsers to identify client side issues.
If none of the above helps, you can try debugging on the network level with tcpdump and Wireshark. This helps identifying protocol and connection issues.
I'm writing a node.js socket.io websockets application (version 1.3.7 of socket.io), and about 75% of the time the client takes a long time to connect to the server - the other 25% of the time it connects pretty much instantly. I've enabled debugging on both the server and the client, and it hangs in both places at the same spot:
Server Log
Client Log (Chrome)
Eventually it will connect, and I've been able to make it connect faster by reducing the timeout from the default of 20 seconds to about 5 seconds, but I'm not sure why it's hanging in the first place. Watching the Chrome network tab, it seems like when a connect attempt is made it will either work immediately or it won't work for the rest of the connect attempt. So dropping the timeout to 5 seconds just means it will make more attempts faster, one of which will eventually succeed.
Network Log (Chrome)
In this case it took 5 connection tries, about 20 seconds, to connect.
Client Code
// client.wsPath is typically http://127.0.0.1:8080/abc, where abc is the namespace to connect to.
client.socket = io.connect(client.wsPath, {timeout: 5000, transports: ["websocket"]});
Server Code
var express = require("express");
var io = require("socket.io");
var htmlApp = express();
var htmlServer = http.Server(htmlApp);
htmlServer.listen(DISPATCH_SERVER_LISTEN_PORT, function()
{
log.info("HTML Server is listening on port " + DISPATCH_SERVER_LISTEN_PORT);
});
var wsServer = io(htmlServer, {transports: ["websocket"]});
var nsp = wsServer.of("/" + namespace);
nsp.on("connection", function(socket)
{
log.info("connect");
};
We've found that clearing the browser cookies can help, but doesn't seem like a permanent solution - is there something that I'm doing wrong?
We are facing similar issue with socket IO SDK. It seems the SDK is actually waiting for the acknowledgement(util is receives the message with SID) to start messaging. But in the typical WS/WSS communication we can start messaging immediately after the connect. We are trying to tweak the SDK in such a way that it can start messaging immediately after connection establishment. Please share if any one has found a better approach.
Anybody else still facing this error?
It is happening to a server I deployed, these are the versions:
"socket.io-client": "^4.5.1"
"socket.io": "^4.5.1"
Sometimes it connects instantly, but others it takes around 1~2 minutes
I have a simple node application which handles GET /foo. This request takes some time to compute and return a file.
Each time the request lasts more than 2 minutes, the connection is closed. I'm using Express 4.10.2 and node 0.10.32.
I read that http module has a default timeout of 2 minutes: http://contourline.wordpress.com/2011/03/30/preventing-server-timeout-in-node-js/
I tried to use:
server.on('connection', function(socket) {
socket.setTimeout(5*60*1000); //5 minutes
});
But even if the connection is not closed after two minutes, when the server tried to send the file back, I got:
{ [Error: Request aborted] code: 'ECONNABORT' }
EDIT:
server.setTimeout(5*60*1000); works fine! Thanks #mscdex
server.setTimeout() is the method that sets the HTTP connection timeout for all connections.
Note: this is not a replicated post for those about settimeout, the key answer here is browser design options.
I am starting study node.js:
A simple example to test async:
var http=require('http');
http.createServer(
function(request, response){
response.writeHead(200);
response.write("Hello, dog is running");
setTimeout(
function(){
response.write("Dog is done");
response.end();
},
10000
);
}
).listen(8080);
console.log("Listen on port 8080")
One interesting thing is its behavior is differernt when in command lind with curl and in browser:
In Ubuntu 12.10, I use curl localhost:8080 in two consoles, they response in almost same 10 sends.
However, I open two browsers, make the request at almost same time, but the whole procedure took me 20 seconds?
thanks.
It's the browser waiting, not node.js
If you run the server and request http://localhost:8080/ in two tabs it takes 20 seconds because the browser waits for the first request to the same url before starting the second.
If you run the server and request http://localhost:8080/1 and http://localhost:8080/2 in two tabs it takes 10 seconds again.
I'm trying to serve long polling requests for 60 secs using node.js. The problem I'm facing is, the browser is getting timed out. The same setup is working for 30 secs. Can anybody suggest how to achieve this? Using JQuery as JS framework.
Thanks...
By default, node.js has a 60 second timeout for TCP/IP connections. You can get around this by explicitly setting the timeout. Here's a quick example:
http.createServer(function (req, res) {
// Connection now times out after 120 seconds
req.connection.setTimeout(120000);
// ... TODO: server logic ...
}).listen(8000);
You can tell node to hold the connection open indefinitely by setting to the timeout to 0. Also, note that the default 60 second timeout applies to all socket connections in addition to TCP/IP.