When using COMET, always remember to flush();

Okay, I couldn’t resist. Since I’ve been speaking so often on the wonders of HTTP Streaming in AJAX applications.

I posted recently about a technique to hold open such a connection using hidden IFRAMES and inline javascript tags.

Since a user agent executes javascript as it’s received, regardless of the rendering status of the rest of the page, one can create a server process that spits out inline javascript tags when a certain event happens on the server. The client interprets the javascript as soon as it appears and now you have a server process calling javascript functions! Kind of like the inverse of AJAX.

Combine that with some slick inter-process communication, and AJAX for client->server data, and you have a very low-latency client-server interaction.

Here’s the issue, if you’re using PHP, one must remember that PHP waits until a script is completed before it echos data to the client. Not very helpful in a streaming environment. Therefore, it is necessary to use a flush() call after every instance of echo that you want to be sent to the browser in real-time. You can configure PHP to behave this way all the time, but it’s seriously sub-optimal and should basically be used only for debugging.

So there you have it, client->server with AJAX, server->client with COMET, and server< ->server with traditional UNIX IPC.

Leave a Reply




You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>