I'm trying to program long polling functionality in Laravel, but when I use the sleep() function, the whole application freezes/blocks until the sleep() function is done. 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 the 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'm using: PHP 5.5 and Apache 2.2.22
The problem doesn't seem to occur outside Laravel (in none Laravel projects).
Thanks in advance.