I am trying to program long polling functions in Laravel, but when I use the sleep () function, the whole application freezes / locks until the sleep () function is executed. Does anyone know how to solve this problem?
My javascript looks like this:
function startRefresh() { longpending = $.ajax({ type: 'POST', url: '/getNewWords', data: { wordid: ""+$('.lastWordId').attr('class').split(' ')[1]+"" }, async: true, cache: false }).done(function(data) { $("#words").prepend(data); startRefresh(); }); }
And PHP:
public function longPolling() { $time = time(); $wordid = Input::get('wordid'); session_write_close(); //set_time_limit(0); while((time() - $time) < 15) { $words = Word::take(100)->where('id', '>', $wordid) ->orderBy('created_at', 'desc')->get(); if (!$words->isEmpty()) { $theView = View::make('words.index', ['words' => $words])->render(); if (is_object($words[0])) { $theView .= '<script> $(".lastWordId").removeClass($(".lastWordId").attr("class") .split(" ")[1]).addClass("'.$words[0]->id.'"); </script>'; } return $theView; } else { sleep(2); } } }
I use: PHP 5.5 and Apache 2.2.22
The problem does not occur outside of Laravel (in no Laravel project).
Thanks in advance.
long-integer php apache laravel polling
Andreas Frejlev Rytman
source share