So I’m doing some research into, and will probably be using COMET for a project I’m working on. Yes, I know that the Apache-PHP combination is not the best for COMET, however this will be on an administrative dashboard with one or two simultaneous users at most. I’m no COMET expert – this is my first foray into the technology. If I learn amazing things while doing a live implementation I’ll write more articles.
While searching I ran across several tutorials and some code, but nothing that was a simple 2 file “Here’s how it’s done” example.
- If the script that’s referenced by your iframe doesn’t output enough data before it starts outputting <script> tags, you’ll see a delay until some sort of buffer is filled up. I’m blaming Firefox, but I’m not certain as I didn’t investigate further. You’ll notice a bunch of white space in my sample code – that’s a whole ton of spaces to stop the delay I was seeing in Firefox.
- On localhost this is very fast. I had to introduce a usleep into the code so that it wouldn’t be so fast as to be unusable. I haven’t tested against a remote web server, but I can assure you this will be dramatically faster than ajax polling.
- You must output the opening and closing script tags each time you want to update the browser. Leaving them out will cause the browser to wait until the request is loaded – the opposite of what we’re trying to accomplish.
The example code is not an infinite loop – it’s a long running script (just a very long for loop). In practice I’ll be using other factors to determine when to break out of an output loop. The project requirements call for this to be running for hours so we’ll find out if that tests the limits of what this approach can handle.