Ghost Ghost - 3 years ago 271
PHP Question

Express drops connection after 2 minutes

I have a PHP Script that connects to Express (Node) running a

setInterval
.
This is being accessed through cUrl, so it'll look something like this:

$status = curl(http://example.com/status?id=xyz);
$status = json_decode(status, true);
if ($status[success] == true) {
//Do stuff here
}


Express is picking the request up the following way:

app.get('/status/', function (req, res) {
var id = req.query['id'];
var statusInterval = setInterval(getStatus, 30 * 1000)
var statusTimeout = setTimeout(function() { //Clears Interval after 15m
clearInterval(statusInterval)
}, 15 * 60 * 1000);
});


however, after 2 minutes the connection randomly drops and
else
is being executed.
Setting
max_execution_time
in the PHP.ini to 600 (10 Minutes) didn't help, nor did
CURLOPT_TIMEOUT
or
CURLOPT_CONNECTTIMEOUT
.

Answer Source

It turns out what Yves had suggested was correct - although not properly mentioned in the docs before, there is a timeout set in place by express that closes connections after 120 Seconds thorugh a setTimeout.

To prevent this, simply void the timeout by adding

res.connection.setTimeout(0);

to your function.

The issue is described - in a more detailed manner - on https://contourline.wordpress.com/2011/03/30/preventing-server-timeout-in-node-js/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download