How to prevent a user from waiting for slow API calls?

I am currently working with a really very slow API, and in many cases site users have to wait until they run out. For instance. when the contact form is submitted and the information is sent via the API.

Now I wonder how can I speed up the API call, at least for the user? Is everything all right, make an asynchronous AJAX call to a separate PHP file and call the API call from there? If so, what happens if the user closes the page and the API call is still running? He might think that everything has already been sent.

+4
source share
2 answers

Everything is all right, make an asynchronous AJAX call to a separate PHP file and make an API call from there?

Yes, definitely; that would be the best way.

If so, what happens if the user closes the page and the API call still works? He might think that everything has already been sent.

It is probably sent; The PHP script executing the API call continues its fun journey, and only when it tries to send a response (confirmation or error) does it find that the client has left. If the API call eventually generates an email, it will complete whether the user will wait or not (if there is no error in the API call).

+5
source

If you have the ability to run cron or a scheduled task, I would convert it to a standalone process. For instance. save the database in db locally and return immediately. Then write a script that will be run periodically through cron to process new entries.

I wrote a blog article about this unnamed call that describes pretty much this exact process: Creating a scalable queuing system with PHP

0
source

Source: https://habr.com/ru/post/1434116/


All Articles