Is it possible to do ajax polling in WordPress with admin-ajax.php for example every 300ms? I have tried this and it appears that doing the ajax request with admin-ajax takes longer than the setTimeOut interval. I have a recursive function with setTimeOut for the purposes of polling a session variable set in the php in order to display a progress bar on the front end, so the interval needs to be pretty quick. As an alternative approach I was able to do an ajax request to a stand-alone php file that echoes the session variable. This doesn't seem to be the prescribed way to do ajax in WordPress. It's working on my system, but another tester is getting an error (404, even though the path is correct).
1 Answer
You idea is equivalent to DDOSing your site, and the difference between the methods is only how fast you will bring the site down.
Wordpress AJAX handling is expensive, and should be limited to user initiated actions. Your second method will be more resilient but you are still going to be wasting server resources as initializing PHP by itself has cost, and usually the server is configured on handle on a limited amount of requests in a time, and your "polling" will leave less available "connection slots" for actual page views.
-
Thanks for the info. Very helpful. I decided to go with a different approach. I'm creating a "faux" progress bar that's based on some numbers I have available within the javascript that will work for showing the progress. Not completely accurate but will do the job in terms of UX.– egauvinCommented Aug 27, 2018 at 3:00
-
IMHO no progress indicator is reliable, and rarely users understand what do they actually mean (I have no idea what it means when windows say that 30% of an update is completed, is it 30% of software line, of 30% of estimated install time). Unless there is some user actionable information displayed as part of the progress bar, it is just an indication that the software is still working, therefor accuracy is over valued here. Commented Aug 27, 2018 at 3:53