Best solution for calling 10 APIs while keeping a good user experience?
I have a site similar to Expedia and Priceline. The user enters dates and then 10 different APIs are called to return results. Getting results from all 10 can (at times) take up to 90 seconds.
What I'd like to hav开发者_运维知识库e is the splash/loading screens (similar to Priceline) to display while the user waits for results.
I have tried using jQuery Ajax to call the APIs and generate results, but some browsers experience issues (locking up when results aren't fast enough).
I'm on a LAMP environment and I've thought of using a cronjob to process the APIs and save the results in a database, but I'm not sure how practical that would be.
- All your ajax calls need to be asynchronous so the user interface stays alive. That should be obvious, but since you mentioned browser lockup, I figured I better mention this.
- You may need to find out if browsers have any per browser window limitations on the number of concurrent ajax calls they will allow. If they do and the number is less than you want to have, you will have to rethink things.
- You definitely want to put up some "Loading results..." type of indicators when waiting for results.
- If your data is the kind of data that you can show progressive results (e.g. show some new info when each ajax call returns), then the user will feel like the function is more responsive than if you wait for all 10 to finish before showing anything.
- For anything as long as your calls are taking, the user should have an easy way to stop the transaction and return to a useful place in your site (without having to kill the browser window or leave your site). You want even impatient users to at least try again rather than leave.
- If you run into browser imitations on simultaneous connections, you might want to have your server proxy the request. Send a small number of request to your server and have it, in turn, fire off as many total requests as are needed and return the collated data.
- If the data being requested can be effectively cached or pre-fetched on your server without influencing accuracy, then that might really help performance.
- Make sure you handle all error conditions on the ajax calls so the user experience at least allows the user to try again or at best continues on with fewer results if some connection doesn't work.
- Understand what all connection timeouts are set to in your ajax code and make sure they are appropriate for your application. If the default connection timeout time isn't long enough to encapsulate most of your ajax calls, you may need to lengthen it.
- Design for the condition where one or more of the ajax calls fails. Know what is going to happen in your app and test for that condition to see that the desired user experience actually works.
- Be aware that (when using asynchronous Ajax calls) the user can interact with your page so you may need to disable, hide or otherwise prevent interaction with parts of the page that they shouldn't fiddle with during the asynch calls.
- The other side of the previous point is that you may want to allow certain types of interaction while waiting for the async calls to complete. For example, you may want to allow the user to change their mind and start a new request. You may want them to interact with some things in the next step in the process while they are waiting, etc... Basically, the more you can offer them while waiting in terms of both progress info, partial results or something else to work on, the less they will notice or mind waiting.
Have you tried something like beanstalkd? It seems like if the latency is up to 90 seconds, you should queue up the jobs and notify the user when the result is ready rather than run the jobs synchronously and display a long-running splash page. To implement push notification, try jQuery Comet.
If u need to show all results at once, fire a single call to a php page and let it do the calls via curl. Remeber set time limit though.
Else, split the work up between two to four pages and fire ajax calls to each. As each returns, show those results. In this case, a simple loading image will suffice. No splash here.
And remember to cache what you can.
***Edit
Note that I'm focusing on php doing the api calls here. Your server with its T1+ connection is definitely going to handle 10 calls faster than my dialup. :).
精彩评论