title: LONG POLLING (in php) starts hanging after 5 requests to database
body: <p>I'm developing <strong>live auction</strong> site, with <strong>real-time</strong> bidding system. I'm using <strong>LONG POLLING</strong> when user bids on item. I've chose long polling because <strong>WebSockets</strong> are not yet very supported and <strong>NODEJS</strong> is to complicated for me to implement right now. So I'm stuck with this simle ajax long polling, which works great for about five bids.</p>
  
  <p><strong>So the problem is:</strong> Bidding works great for 5-6 item bids in 1 second intervals (I get instant response from server-ajax), but the 7th (click) on bid button this response-long polling hangs for about 16-22 seconds and then completes the request. At the end everything is updated in database and completed but after every 5-6 bids response/ajax call hangss for about 16-22 seconds. </p>
  
  <p><strong>How could I reduce this time, that everything should go smoothly no matter how many times user bids, without lag...</strong></p>
  
  <p><strong>I'm using Apache/PHP/MySql on localhost/wamp</strong></p>
  
  <p>My code:
  <strong>index.php</strong></p>
  
  <pre><code>&lt;script type="text/javascript" charset="utf-8"&gt;             
  var old_timestamp = &lt;?php echo $old_timestamp;?&gt;; //here i'm echoing last timestamp of         auction
  
  function waitForMsg(){
  jq.ajax({
  type: "POST",
  url: "http://localhost/bid/comet/poll.php",
  data: {"old_timestamp" : old_timestamp},
  async: true,
  cache: false,
  
  success: function(data){
  var json = eval('(' + data + ')');
  if(json['msg'] != "") {
  jq('#comet_display').html(json['msg']); //here I show ID of which auction item was bidded
  }
  old_timestamp = json['old_timestamp']; 
  setTimeout('waitForMsg()',100);
  },
  error: function(XMLHttpRequest, textStatus, errorThrown){
  
  setTimeout('waitForMsg()',1000);
  }
  });
  }
  
  jq(window).load(function(){ 
  waitForMsg();
  jq("#a_loader").show();
      var url = "http://localhost/bid/auctions-ajax"; // the script where you handle the form input.  
      jq.ajax({
             type: "POST",
             url: url,
             data: {au978 : true}, 
             async:false, //to sem dodal za časovni zamik
             success: function(data)
             {
               jq("#a_loader").hide();  
               jq("#show-category").html(data); // show response from the php script.              
             },
              error: function(result) {
                  jq("#show-category").html("Sorry, something went wrong. Please try again later.");
              }
      });
  
  });
  
  function bid(id){
  
  var url = "http://localhost/bid/comet/update-auction.php"; // the script where you handle the form input.
  var user_id=&lt;?php echo $user_id;?&gt;; //user id from session
  jq.ajax({
      type: "POST",
      url: url,
      data: {"auct_id" : id, "user_id" : user_id}, // serializes the form's elements.
      success: function(data)
      {
          //it updates in user table its remaining number of bids 
      },
      error: function(XMLHttpRequest, textStatus, errorThrown){
          alert("Something went wrong. Click OK to refresh.");
  
      }
  });
  }
  
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>poll.php</strong></p>
  
  <pre><code>&lt;?php
  
  if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  require_once($_SERVER['DOCUMENT_ROOT'] . 'bid/include/DEFINES.php');
  require_once(CLASS_AUCTION);
  require_once(CLASS_DB);
  
  $a=new Auction();
  
  $old_timestamp = $_POST['old_timestamp']; 
  $bidded_id=0;
  $db=DB::getInstance();  
  $sql="SELECT timestamp, id FROM auction ORDER BY timestamp DESC LIMIT 1";   //desno doda tabelo kategorija  
  $stmt=$db-&gt;db-&gt;prepare($sql) or die("Prepare Error");   
  $stmt-&gt;execute();   
  $result2=$stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
  foreach ($result2 as $rez){
      $current_timestamp=$rez['timestamp'];   
      $bidded_id=$rez['id'];
  }
  $stmt-&gt;closeCursor();
  
  
      while($current_timestamp &lt;= $old_timestamp){
      usleep(1000);
      clearstatcache();
      $db=DB::getInstance();  
      $sql="SELECT timestamp, id FROM auction ORDER BY timestamp DESC LIMIT 1";       
      $stmt=$db-&gt;db-&gt;prepare($sql) or die("Prepare Error");   
      $stmt-&gt;execute();   
      $result=$stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
      foreach ($result as $rez){
          $current_timestamp=$rez['timestamp'];   
          $bidded_id=$rez['id'];
      }
      $stmt-&gt;closeCursor();
  
  }
  $response = array();
  $response['msg'] = 'BID na avkciji: '.$bidded_id;
  $response['old_timestamp'] = $current_timestamp;
  echo json_encode($response);
   }else{
  require_once($_SERVER['DOCUMENT_ROOT'] . 'bid/errors/404.html');
   }
  ?&gt;
  </code></pre>
  
  <p><strong>and the update-auction.php</strong></p>
  
  <pre><code>&lt;?php
  if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  require_once($_SERVER['DOCUMENT_ROOT'] . 'bid/include/DEFINES.php');
  require_once(CLASS_AUCTION);
  require_once(CLASS_USER);
  require_once(CLASS_DB);
  $u=new User(); 
  $a=new Auction();
  $auction_id=$_POST['auct_id'];
  $user_id=$_POST['user_id'];
  $date = new DateTime();
  $timestamp=$date-&gt;getTimestamp();
  $a-&gt;updateAuction($auction_id,$user_id,$timestamp/*,$bid_price,$bids_spent,$total_paid*/);
  $u-&gt;updateUserBids($user_id);
  
  }else{
  require_once($_SERVER['DOCUMENT_ROOT'] . 'bid/errors/404.html');
  }
  ?&gt;
  </code></pre>
  
  <p><em>Thank you for viewing my problem!</em></p>