Is it possible for PHP to send responses at regular intervals to Javascript?
I have a site built in PHP that has both a live copy and a test/sandbox copy. Once I’m done tinkering on the sandbox site and I’m happy with what I’ve got, I click a button with an onclick
attribute to have the code copied over to the live site via a Javascript function. My code works fine when Javascript contacts the server-side PHP function with a fetch/then/catch block of code:
await fetch(_url_of_site_and_name_of_PHP_file_receiving_the_javascript_request, {
method: 'POST',
body: 'mirrorsites',
headers: { 'Content-type': 'application/json; charset=UTF-8', },
}).then(response => response.text())
.then(data => {
console.log(data + '% of your site is done.');
}).catch(error => {
console.log('Whoops. Something went wrong.');
});
but it takes a few minutes to copy over, and the user is left wondering “…Hey, site, are you working, or is something frozen?”
So I’d like to adapt the code so that PHP can return periodic updates every 10, 15, 20, whatever seconds, something like “Hey, we’re 10% done…20% done…” etc. until we reach 100%. My PHP code is a basic function that processes, finishes up, and then returns a success/error value with a run-of-the-mill return $returnvalue;
line of code. The gist is it takes stock of all the files in the sandbox site and then loops through each file and uses a PHP copy
command to copy them to the live site. It doesn’t exit the function until the loop is done. (It doesn’t erase any files on the live site if they’re missing from the sandbox site because the live site contains extra files like robots.txt, a sitemap, a favicon, etc.)
In a nutshell, my code handles one Javascript call and one PHP response to that Javascript call, but I’d like it to handle one Javascript call and regular PHP updates.
I have yet to try anything other than doing Google research because…well…I don’t even know where to start. How can I get PHP to send periodic responses and tell Javascript to expect those periodic responses? Obviously my await
keyword for my fetch will wait until the whole thing is finished. But I’m afraid that, if I remove it, the Javascript code will make the call, keep trucking, and say ‘Okay, you’re 100% done’ while, on the PHP end, the function is still running. Besides, I use that same Javascript fetch/then/catch code to communicate between client and server for things other than the site copy that I need to wait.
I did come across Calling a php script at regular intervals with javascript, but my situation might differ in a couple of ways: (1) my Javascript calls PHP once and expects PHP to respond multiple times (rather than Javascript calling PHP multiple times and expecting a response once per call), (2) PHP is giving Javascript an answer with return $returnvalue;
instead of an echo or print statement like in the other post. Combined with a problem-solving-related/reasoning cognitive dyslexia, the differences and similarities between my problem and the one in the other post are making my head spin.
Can anyone point me in the right direction on how I can do this, or even tell me if I’m going about this in the right way or if I should change my code to match with the other post? I’d rather not change the code since, like I said, the Javascript/PHP communication is used for client/server communication other than copying the site. Thanks!