title: Codeigniter long polling blocking other ajax requests
body: <p>I want to achieve a pseudo real time chat app inside a Codeigniter framework using long polling. I know it's not the best option (I could try with HTML5 Websocket), but I'm on a cPanel shared account so I don't have any privileges on installing anything</p>
  
  <p>So, let's get back to the issue itself.
  Basically, I use a .js file similar to the one below:</p>
  
  <pre><code>    //get server time
      var time;
      $.ajax( {
           url: JS_BASE_URL + 'admin/users/get_time',
           success: function( dataResponse ) {
               time = dataResponse;
          },
          type: 'GET'
      } ); 
      //long polling AJAX 
      function getNewMsgs() {
          $.ajax( {
              url: JS_BASE_URL + 'admin/users/get_new_msgs',
              type: 'POST',
              // send the time
              data: { time: time, sender_id: sender_id },
              dataType: 'json',
              async: true, /* If set to non-async, browser shows page as "Loading.."*/
              cache: false,
              success: function( dataResponse ) {
                  // update the time
                  time = dataResponse.time;
                  // show the new messages
                  dataResponse.msgs.forEach( function( msg ) {
                      preparePost('in', (msg.date_sent), msg.sender_id, 'avatar3', msg.message);        
                  } );
                  // repeat
                  setTimeout( getNewMsgs(), 1000 );
              },
              error: function (XMLHttpRequest, textStatus, errorThrown){
                  setTimeout( getNewMsgs(), 1000 );    
              }                   
          } );
      }
      getNewMsgs(); // this one is on the document ready
  </code></pre>
  
  <p>The php get_new_msgs function in Users controller:</p>
  
  <pre><code>function get_new_msgs() {    
      echo json_encode( array(
          'msgs' =&gt; $this-&gt;aauth-&gt;start_polling($this-&gt;aauth-&gt;get_user()-&gt;id),
          // response again the server time to update the "js time variable"
          'time' =&gt; time() 
      ) );
      die();
  }
  </code></pre>
  
  <p>And the model function start_polling:</p>
  
  <pre><code>function start_polling($receiver_id) {
      // get the time
      $time      = $this-&gt;CI-&gt;input-&gt;post( 'time' );
      $sender_id = $this-&gt;CI-&gt;input-&gt;post( 'sender_id' );
  
      // some crappy validation
      if( !is_numeric( $time ) ) {
          return array();
      }
  
      $time = date('Y-m-d G:i:s',$time);
      // -&gt; 2010-10-01
      //$time = $time['year'] + '-' + $time['mon'] + '-' + $time['mday'];
  
      while( true ) {
  
          $where = 'date_sent &gt;= "' . $time . '" AND ((receiver_id=' . $receiver_id . ' AND pm_deleted_receiver IS NULL AND sender_id=' . $sender_id . ' AND pm_deleted_sender IS NULL) OR (receiver_id=' . $sender_id . ' AND pm_deleted_receiver IS NULL AND sender_id=' . $receiver_id . ' AND pm_deleted_sender IS NULL))'; 
          $query = $this-&gt;aauth_db-&gt;where($where);
          $query = $this-&gt;aauth_db-&gt;order_by('id','ASC');
          $query = $this-&gt;aauth_db-&gt;get( $this-&gt;config_vars['pms']);
          //die($this-&gt;aauth_db-&gt;last_query());
          if( $query-&gt;num_rows() &gt; 0 ) {
              $result = $query-&gt;result();
              if ($this-&gt;config_vars['pm_encryption']){
                  $this-&gt;CI-&gt;load-&gt;library('encrypt');
  
                  foreach ($result as $k =&gt; $r)
                  {
                      $result[$k]-&gt;title   = $this-&gt;CI-&gt;encrypt-&gt;decode($r-&gt;title);
                      $result[$k]-&gt;message = $this-&gt;CI-&gt;encrypt-&gt;decode($r-&gt;message);
                  }
              }
              echo json_encode($result);
              die();
  
  
   } else {
          sleep( 1 );
          continue;    
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Now, the issue is that whenever I run the page with the chat, a request is loaded and waiting for a response from the server. But, no other requests will finish.. it seems that the other requests sent after the long polling are waiting for that first one to finish.. Even if I stop the script, the page will load very slow..</p>
  
  <p>Any idea on what I'm missing?
  Thank you in advance!</p>