Welcome to the wonderful world of persistent connections. First featured in Netscape's Amazing, Continuously Refreshing Fish Cam, this special type of connection will remain open to send a stream of data. Basically, it takes advantage of a little-used content type. It's been working just fine in Netscape since version 1.1, so don't be afraid to implement it; even those people who've never upgraded their browsers can use it. Unfortunately, however, Microsoft's Internet Explorer 3.0 does not support it.
We'll be using the following content-type header:
Content-type: multipart/x-mixed-replace; boundary=somemagicalboundarystringEach part will be delimited with the string "somemagicalboundarystring". 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 effect will be the following, including all the HTTP headers:
HTTP/1.0 200The 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.
Content-type: multipart/x-mixed-replace; boundary=magicalboundarystring--magicalboundarystring
Content-type: image/jpegJPEG IMAGE STREAM GOES HERE
--magicalboundarystring
Content-type: image/gifGIF IMAGE STREAM GOES HERE
--magicalboundarystring
Content-type: text/plainHi. This is a sample plain text file
--magicalboundarystring
Web servers can be notoriously difficult to set up and maintain. So, you'll have to figure out how to implement a subtle feature called "non-parsed" CGIs on your site. Normally, the web server will buffer all output from your CGI program until it 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. So, save the following Perl script with a name like "nph-serverpush.pl" and try it out. Just be sure to change the path in "/some/path/to/images/and/stuff/" to a directory on your server that's world readable. (It should be the absolute path, not a virtual one.) This script will read each file in that directory. If the file's extension is .html, .txt, .jpeg, .jpg, or .gif, it will be sent and wait 5 seconds before sending the next one.
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.
Source Code
Author: Doug Steinwand
Date: 08/12/1997