php - Long polling in Laravel (sleep() function make application freeze) -


i'm trying program long polling functionality in laravel, when use sleep() function, whole application freezes/blocks until sleep() function done. know how solve problem?

my javascript looks 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'm using: php 5.5 , apache 2.2.22

the problem doesn't seem occur outside laravel (in none laravel projects).

thanks in advance.

actually not long polling if use bonez code. long polling if connection stays opened (maybe timeout) until server sends response. if client sends every 2 seconds request , gets response, polling , server response 2 seconds late in worst case. long polling don't have delay.

the freezing problem no error laravel. session blocks. use session_write_close(); before calling long polling method or use cookie session driver. more information please see http://konrness.com/php5/how-to-prevent-blocking-php-requests/


Comments

Popular posts from this blog

php - get table cell data from and place a copy in another table -

javascript - Mootools wait with Fx.Morph start -

php - Navigate throught databse rows -