title: Simple PHP long polling chat script, too simple?
body: <p>Im working on a simple chat app, probably 10 to 20 users per room.</p>
  
  <p>The Script that queries the database for new messages looks too simple for all the request it'll be getting.</p>
  
  <p>Below is the block of code that loops for new messages, the rest of the script is just getting the variables, construction of the query and the json response object:</p>
  
  <pre><code>$sleepTime = 1; //Seconds
  $data = "";
  $timeout = 0;
  
  //Query database for data
  while(!$data and $timeout &lt; 10){
      $data = getQuery($sql);
      if(!$data){
          //No new messages on the chat
          flush();
          //Wait for new Messages
          sleep($sleepTime);          
          $timeout += 1;
      }else{
          break;
      }
  }
  </code></pre>
  
  <p>The block above will query the database for new messages every second for 10 seconds, if after the 10 seconds there are no new messages it will notify the browser. The browser waits 5 seconds and then sends another request 
  to get new messages.</p>
  
  <p>However If the script finds new messages, the browser will request more new messages instantly as soon as it gets the response with the new messages from the server.</p>
  
  <p>This process goes on and on...</p>
  
  <p>So how can i optimize this process any further?
  Is this as good as it gets?
  Is working fine on my local server, but im afraid that just a few users could overload a live server(shared host) with all the requests and the loopings. </p>
  
  <p>Here is live DEMO you can check with firebug <a href="http://pixbush.com/chat/chat.php" rel="noreferrer">http://pixbush.com/chat/chat.php</a></p>