1

I wrote some PHP code to help me connect to a REST API for a telephone system (ie. ICWS.php.)

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.

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

    while(1){
        //process Messages
        $icws->processMessages();
        //show the Calls Queue
        $result = $icws->getCallsQueue();
        //$current = $icws->getCurrentUserStatusQueue();
        echo 'event: getMessagingQueue' . "\n";
        echo 'data: ' . json_encode( array('calls' => $result));    
        echo "\n\n"; //required  

        ob_flush();
        flush(); 
        putToSleep($sleepTime);

    }
function putToSleep($val = 1){

    if( is_int($val) ){
        sleep($val);
    } else {
        usleep($val * 1000000);
    }
}

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.

Here is my code that generates the server-side polling

    //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();
    });

The problem that I am facing is that when making an ajax call, the response takes way too long (+1 minute).

It seems that the Apache server slows down as using other application becomes a little slower.

What can I check and how can I trouble shoot this problem?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Jaylen
  • 39,043
  • 40
  • 128
  • 221
  • 1
    Monitor the server load. Is your daemon (`poll.php`) behaving properly? `putToSleep` seems suspect. – Halcyon May 26 '15 at 17:40
  • 1
    What is the purpose of infinite while loop??? – A. Wolff May 26 '15 at 17:40
  • the infinite loop is to make the server-side polling. I have updated my question to show the putToSleep code. – Jaylen May 26 '15 at 17:43
  • Can't help but wonder if your timeout period corresponds to the time taken for a response. See here: http://stackoverflow.com/questions/13572099/what-defines-the-timeout-period-in-php – enhzflep May 26 '15 at 17:43
  • @Halcyon how can I monitor the server load on windows server? – Jaylen May 26 '15 at 17:53
  • Hi. It seems to me that you don't need make infini loop and sleep. when you make a call via Ajax the server create the thread and load the code. And it's not a cade that in a sleep but new one . So for now you have a tons of threds in sleep, ZOMBIES. I sujest to remove the code from a while and remove the sleep, reboot the Apache if you can – volkinc May 26 '15 at 18:37
  • @volkinc this is call server-sent events. please check this URL http://stackoverflow.com/questions/30279887/how-to-fully-implement-server-sent-events-for-message-polling and https://developer.mozilla.org/en-US/docs/Server-sent_events/Using_server-sent_events – Jaylen May 26 '15 at 18:53
  • well, yea. only thing is access to DB if you have one in a getCallsQueue – volkinc May 26 '15 at 19:05

0 Answers0