0

I don't know if I am doing something incorrect or if Twilio is just naturally slow at requesting records. When requesting a list of conferences with php the response can take up to 5-7 minutes which doesn't seem viable for any type of application.

Here is a sample of the code I am using:

$conferences = $client->account->conferences->getIterator(0, 50, array(
    ));
    foreach ($conferences as $conference) {
        $conferenceRoom = $client->account->conferences->get($conference->sid);
        $date1 = new DateTime($conference->date_created);
        $date2 = new DateTime($conference->date_updated);
        $interval = $date1->diff($date2);
        $page = $conferenceRoom->participants->getPage(0, 50);
        $participants = $page->participants;
        $participantCount = count($participants);
        $result['conferences'][$conference->sid]['friendly_name'] = $conference->friendly_name;
        $result['conferences'][$conference->sid]['sid']           = $conference->sid;
        $result['conferences'][$conference->sid]['participants']  = $participantCount;
        $result['conferences'][$conference->sid]['status']        = $conference->status;
        $result['conferences'][$conference->sid]['duration']      = $interval->format('%H:%I:%S%');
        $result['conferences'][$conference->sid]['date_created']  = strtotime($conference->date_created);
        $result['conferences'][$conference->sid]['date']          = $conference->date_created;
}
echo json_encode($result);
Chaosjosh
  • 137
  • 1
  • 13

2 Answers2

1

Twilio developer evangelist here.

You're using the getIterator method for the conferences resource in the PHP library. getIterator returns an interator that handles pagination for you, so as long as you continue looping through it, it will keep requesting pages from the Twilio API. My guess is that you have had a lot of conferences so you keep paging for a while.

Also, for each conference you then make an API call for the conference resource and another API call for the participants in the call. Even if you only have 10 conferences in your account, you will be making 30 API calls.

So, whilst Twilio is not taking 5-7 minutes to return one response, your script is doing an awful lot of work with the API that is taking a long time.

I agree with Half Crazed's (great name by the way) suggestion of caching results. You could update a conference object in your own system by setting up an event callback URL for your calls so that Twilio can send you status updates about the conference call and you can save the details about it at that point.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Cool thanks! I set up an event callback URL on the conference to store the info into a database. As well as created a smaller separate API call to retrieve in-progress conferences – Chaosjosh Jul 21 '15 at 17:47
0

Although I see nothing wrong with the code, when dealing with an API, it's usually best to have a separate service (such as a CRON job, or separate thread) to call the API and cache the results. Your live application would then read from the cached results... this way the speed of your site is unaffected by 3rd party sites. It's probably a good idea to display a small note, something along the lines of "Results last updated on XYZ"

There's a ton of reasons why slow-loading pages are bad.

Rob W
  • 9,134
  • 1
  • 30
  • 50