Once in a while, I do training courses. I made a web page that allows me to send info that is subject to change to the participants. Right now this is all done with polling in Javascript, but the problem there is that either the polling rate needs to be quite high, or it takes a while for all the users to see the most recent info.
So what I want to do is to poll relatively infrequently, but for the AJAX calls that are handled on the back end by a PHP script to hang around until updated information comes along. I just can’t figure out a good way to do this. The critical part here is waking up a PHP script that is currently de facto asleep.
I do have something working with SystemV message queues. But there are two issues with that. I can send messages to an arbitrary receiver, but just not to all receivers. Right now I just send a message to any receiver, and that receiver repeats the message, so eventually they should all get it. But that seems to brittle to me. An alternative is keeping track of all the receivers... Also, with blocking message reads, there are no timeouts, so it’s possible for large numbers of these scripts to get stuck and eat up memory on the server.
I also looked at signals. This seems to work well enough when I run PHP scripts through the command line, but somehow the signals never reach the scripts when they run through the web server. And I’d have to keep track of the PIDs of the different scripts, with the possibility that signals that end up on a thread that isn’t running this particular script might get interrupted.
Ideally, I’d just point PHP to a file and tell it to wake up when that file changes, or after x seconds if it never changes.
I know web sockets would solve this, but then I’d have to come up with server software to handle those, which seems like a larger project than I’d like.
Suggestions?
So what I want to do is to poll relatively infrequently, but for the AJAX calls that are handled on the back end by a PHP script to hang around until updated information comes along. I just can’t figure out a good way to do this. The critical part here is waking up a PHP script that is currently de facto asleep.
I do have something working with SystemV message queues. But there are two issues with that. I can send messages to an arbitrary receiver, but just not to all receivers. Right now I just send a message to any receiver, and that receiver repeats the message, so eventually they should all get it. But that seems to brittle to me. An alternative is keeping track of all the receivers... Also, with blocking message reads, there are no timeouts, so it’s possible for large numbers of these scripts to get stuck and eat up memory on the server.
I also looked at signals. This seems to work well enough when I run PHP scripts through the command line, but somehow the signals never reach the scripts when they run through the web server. And I’d have to keep track of the PIDs of the different scripts, with the possibility that signals that end up on a thread that isn’t running this particular script might get interrupted.
Ideally, I’d just point PHP to a file and tell it to wake up when that file changes, or after x seconds if it never changes.
I know web sockets would solve this, but then I’d have to come up with server software to handle those, which seems like a larger project than I’d like.
Suggestions?
Last edited: