How to trigger an event sent by the server in HTML5 ... OR: Can a PHP script know if another script has been called?
Now that all the browsers I like have almost full Server Sent Events support, I wanted to try and implement it on a site that I was putting off because I hate polling. But I started to doubt that I was hoping that I could help.
Here is my use case:
The user navigates to a form, something time-based and competitive, in this case a class registration. All things being equal, they have a list of about 30-40 classes that they are eligible for, and in order to minimize the time she logged in, but he hit first, but he didn't want to hit, but she already selected another class "etc., I want to make the form live so that when someone selects an option, it goes straight to the db and anyone else viewing the form sees it filling out (I will deal with stressing people who change their minds later).
So, in a polling script, I had to deal with AJAX calls that had to check the status of 40 points and update them and set an interval that could potentially create conflicts.
But with server-dispatched events I can get the listener to only get the spots that need updating, which seems better, but here's where I'm stuck:
-
Is there a risk of overloading the listener? Let's say the script sends 15 messages related to state change. I see vague mentions of how user agents should handle queued tasks, but it is unclear if this is appropriate for establishing a connection or handling messages sent by the server
-
Is it basically just shifting the burden of polling from the browser to the server? Does the script need to check the DB every second for changes? Is there a way for the script to know or notify when a change has occurred? Let's assume that requests to a place are sent to
requests.php
via ajax and that itupdates.php
returns events back to the browser. Is there a standard and / or clever way toupdates
stand by untilrequests
a commit is made?
The only solution I can think of is to requests.php
write the committed changes to a flat file (maybe commits.xml
), and updates.php
just checks the file size every half second, thus keeping the workload to a minimum.
Any better / smarter / more obvious solutions?
source to share