panique / php-long-polling

An extremely simple example of a "real-time" self-updating page using long-polling.
425 stars 132 forks source link

I got a 504 error in 10 minutes of script work #12

Open bicdibuss opened 6 years ago

bicdibuss commented 6 years ago

I have a php5.6 nginx 2GB Ram on my server. On localhost it works good. What the problem can it be?

ionurboz commented 6 years ago

Are you more open-minded than you mean?

mitchobrian commented 6 years ago

@ionurboz :) @bicdibuss maybe Same-Origin-Policy ?

bicdibuss commented 6 years ago

Im building a chat using this script on localhost all works good, but when I moove scrips to the server it works only about 10 minutes then in console of browser I see 504 error and script stop work. After reloading page it works again the same time.

ionurboz commented 6 years ago

What your application name? Maybe the localhost application blocked more live connection?

bicdibuss commented 6 years ago

On localhost it works good. On server even example script got error. May be I need some configs of nginx or php?

ionurboz commented 6 years ago

Maybe this is the root of the problem max_execution_time.

php.ini edit max_execution_time.

Defaults:

max_execution_time | "30"

try repeating the same things 30 seconds later without refreshing the page

if you do not find a solution usleep (); investigate

bicdibuss commented 6 years ago

I have set max_execution_time=3600 but it dosent help server.php?timestamp=1527239428 504 (Gateway Time-out)

panique commented 6 years ago

it's probably a 600 seconds limit on nginx side :)

sunnz commented 4 years ago

I am getting 504 error from nginx too when it reaches its time limit on how long it would wait for a response from php-fpm.

What's the best practice?

For now I put a time limit into the PHP script, so it would return a json object before the nginx time limit kicks in, then javascript would send another request to the server.

IE

$start_time = time();
$time_limit = 60; // set this to be less than the nginx time limit
// main loop
while (true) {
    ...
    $time_lapsed = time() - $start_time;
    if ($time_lapsed >= $time_limit || $last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {
        ...
        // leave this loop step
        break;
        ...

By doing this it is combining short-polling and long-polling... but not much choice when you can't control nginx limits from PHP I guess?