Pushing Content -- Really

Even though the concept of "Channels" in the latest web browsers still hasn't really caught on, the idea of "Pushing" content to the client's browser is quite interesting. Here I want to discuss how to use a persistent connection between the web browser and web server to continuously send new data.

The special CGI script below will send every file in the requested directory to the client's web browser, with each document replacing the one before it. A technology similar to this is used at The United Space Alliance to provide real-time video feeds of NASA-TV in your web browser. No plugins are required to receive the video. Also, take a look at Netscape's Amazing, Continuously Refreshing Fish Cam.

These examples make use of the MIME type "multipart/x-mixed-replace" to encapsulate many different files into one. This has been working fine in Netscape since version 1.1. Unfortunately, Microsoft's Internet Explorer 3.0 does not support it. (Maybe 4.0 does... but who's upgraded to that behemoth?)

We'll be using the following content-type header: Content-type: multipart/x-mixed-replace; boundary=xstringx

Each part will be delimited with the string "xstringx". For obvious reasons, this string should not appear in any of the output documents. It should be noted that each part of the multipart document will replace the previous one in the web browser. So, the data sent by the web browser will be the following:

HTTP/1.0 200
Expires: Thu, 1 Jan 1998 00:00:00 GMT
Content-type: multipart/x-mixed-replace; boundary=xstringx

--xstrinx
Content-type: image/jpeg

JPEG IMAGE STREAM GOES HERE
--xstringx
Content-type: image/gif

GIF IMAGE STREAM GOES HERE
--xstringx
Content-type: text/plain

Hi. This is a sample plain text file
--xstringx

The x-mixed-replace content type means that each part will replace the previous one. You'll see a series of images, html documents or whatever the content happens to be. Also, the use of the "Expires:" header guarantees that the web browser won't try to cache the document. Every time you reconnect it will have to retrieve the data again.

To use the script below, you'll need to implement a called "non-parsed" CGIs on your site. Normally, the web server will buffer all output from your CGI program until it the program finishes. We don't want that to happen here. With Apache, it's quite easy. If the name of your CGI program starts with "nph-" it won't be parsed. Also, change the glob "/some/path/*" to the path where you want to look for files.


Click here to grab the source code.

Notice how the content types are determined by the file's extension is .html, .txt, .jpeg, .jpg, or .gif. Modern web servers may use other techniques to really what is really inside each file.


Click here to try it out.

If the user hits the STOP button, the HTTP connection will be broken and the Perl script will terminate. You should note that while this script is running, your server has a connection open. This shouldn't be a problem on most web servers which can handle many simultaneous connections.

Another interesting thing to explore is what happens when the data stream is saved to disk. Try it out by right-clicking on "try it out" and then choose save to disk. What ends up in the file might surprise you.

Author: Doug Steinwand
Date: [03/24/98]
More articles about CGI
More articles by Doug Steinwand
Author Biography