title: What is causing a very slow ajax response?
body: <p>I wrote some PHP code to help me connect to a REST API for a telephone system (ie. ICWS.php.)</p>
  
  <p>Then to make my life easier, I wrote a small script (ie. interations.php) that accepts two parameters: a method and an ID. This script will basically call a public method in my PHP connector.</p>
  
  <p>In addition, I have another script (ie. poll.php). This script will ping the API once every half a second to see if there is a new message available. I am using server-side polling to handle this. The code below will show how poll.php </p>
  
  <pre><code>    while(1){
          //process Messages
          $icws-&gt;processMessages();
          //show the Calls Queue
          $result = $icws-&gt;getCallsQueue();
          //$current = $icws-&gt;getCurrentUserStatusQueue();
          echo 'event: getMessagingQueue' . "\n";
          echo 'data: ' . json_encode( array('calls' =&gt; $result));    
          echo "\n\n"; //required  
  
          ob_flush();
          flush(); 
          putToSleep($sleepTime);
  
      }
  function putToSleep($val = 1){
  
      if( is_int($val) ){
          sleep($val);
      } else {
          usleep($val * 1000000);
      }
  }
  </code></pre>
  
  <p>From my site (ie. phonesystem.html) I start server-side polling "which pings the API once every 1/2 seconds." From the same page, I can also make other direct calls (ie. Dial 7204536695); all requests are done via Ajax.</p>
  
  <p>Here is my code that generates the server-side polling</p>
  
  <pre><code>    //Server Side Message Polling
      function startPolling(){
  
          var evtSource = new EventSource("poll.php");
  
  
          evtSource.addEventListener("getMessagingQueue", function(e) {
          var obj = JSON.parse(e.data);
  
            if(!obj.calls || obj.calls.length === 0){
                console.log('no messages');
                phoneKeyPad(false);
                return;
            }
            processMessages(obj.calls);
  
          }, false);
      }
  
  
      $(function(){
           startPolling();
      });
  </code></pre>
  
  <p>The problem that I am facing is that when making an ajax call, the response takes way too long (+1 minute).</p>
  
  <p>It seems that the Apache server slows down as using other application becomes a little slower.</p>
  
  <p>What can I check and how can I trouble shoot this problem?</p>