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.
Related
I currently have a request which is made from an angular 4 app(which uses electron[which uses chromium]) to a bottleneck(nodejs/express) server. The server takes about 10 minutes to process the request.
The default timeout which I'm getting is 120 seconds.
I tried to use setting the timeout on the server using
App.use(timeout("1000s")
In the client side I have used
options = {
url,
method: GET
timeout : 600 * 1000}
let req = http.request(options, () => {})
req.end()
I have also tried to give the specific route timeout.
Each time the request hits 120 seconds the socket dies and I get a "socket timeout"
I have read many posts with the same questions but I didn't get any concrete answers. Is it possible to do a request with a long/no timeout using the tools above? Do I need to download a new library which handles long timeouts?
Any help would be greatly appriciated.
So after browsing through the internet I have discovered that there is no possible way to increase Chrome's timeout time.
My solution to this problem was to open the request and return a default answer(something like "started") then pinging the server to find out it's status.
There is another possible solution which will be to put a route in the client(I'm using electron and node modules in the client side so it is possible) and then let the server ping back to the client with the status of the query.
Writing this down so other people will have some possible patches. Will update if I'll find anything better.
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.
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'm writing a node.js application that needs to talk to a server. It establishes an http connection with the following code:
var client = http.createClient(u.port, u.hostname, u.secure);
client.on("error", function(exception) {
logger.error("error from client");
});
var request = client.request(method, u.path, headers);
I don't see any option in the node.js documentation for setting a timeout on the connection, and it seems to be set to 20 seconds by default. The problem I'm having is that I have users in China on what appears to be a slow or flaky network, who sometimes hit the timeout connecting to our datacenter in the US. I'd like to increase the timeout to 1 minute, to see if that fixes it for them.
Is there a way to do that in node.js?
Try
request.socket.setTimeout(60000); // 60 sec
I think you can do something like:
request.connection.setTimeout(60000)
request.connection returns the net.Stream object associated with the connection.
and net.Stream has a setTimeout method.
There is no capability in Node to increase connect timeout. Since usually connect timeout (i.e. connection establishing timeout) is OS-wide setting for all applications (e.g., 21 seconds in Windows, from 20 to 120 seconds in Linux). See also Timouts in Request package.
In contrast, Node allows to set decreased timeout and abort connecting even in case when the connection is not yet established.
The further timeouts (in case of connection has been established) can be controlled according to the documentation (see request.setTimeout, socket.setTimeout).
You have to wait for the client socket connection to be established first, before setting the timeout. To do this, add a callback for the 'socket' event:
req.on('socket', function (socket) {
myTimeout = 500; // millis
socket.setTimeout(myTimeout);
socket.on('timeout', function() {
console.log("Timeout, aborting request")
req.abort();
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
// error callback will receive a "socket hang up" on timeout
});
See this answer.
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.