Server Push

Navigation

Skip navigation.

Site search

Site navigation

What is it

Server push is a simple way of sending file updates to a browser. It is not true streaming, though it can feel a lot like it when done well. It is quite different from AJAX/XMLHttpRequest or equivalent techniques, because the browser does not need to keep asking for updates. Instead, the server tells it when there is an update, and sends it to the browser.

It is most commonly used in Webcams, to serve a new image to the browser every few seconds, without the browser needing to check if an updated image is available.

To download the script(s), and see the script license, use the links on the navigation panel at the top of this page.

What browsers does it work in

Most major browsers, and some minor browsers support it for images, with one important exception. Those that fail are Internet Explorer, NetFront, ICEbrowser, iCab 3, WebTV/MSNTV and Clue. Escape and Konqueror only support it when the file is viewed directly, not when used in image tags. In theory, server push can be used for HTML files too, but these are not reliable, and not recommended.

Safari always displays frames one frame too late (so each frame will not display until the data for the next frame has arrived), and for some reason does not initially display the first frame in <img> tags, though it does initially display it in <object> tags or when viewed directly.

What this script does

This PHP script provides a simple function to enable server push. All you need to do is tell it what file to serve, and update that file whenever a new version is available. The script also provides a JavaScript-based workaround for Internet Explorer (it may have limited success with other browsers), that uses more bandwidth, but at least it works, and avoids the need for the user to install a plugin - a common failed solution to this problem. All instructions for use are in the PHP library file.

Demo

The following image is pushed to the browser. Note that it never completes loading, and you should not press the 'Stop' button in your browser (if you have already done so, you will need to reload to see the demo). The server is set to scan for updates approximately once every second. In Internet Explorer, the workaround will be disabled after 30 seconds, to avoid using too much bandwidth.

Fallback text

Use the following buttons to change the image file that is stored on the server:

Note that the onerror attribute on images (used to get support for IE) is not part of the HTML 4 specification, even though pretty much all browsers understand it. This is a case of the spec failing to reflect reality, and it is harmless. However, it does mean that this page, and any page you choose to use this on, will not validate.

How server push works

The process is very simple. When the browser requests a file (typically an image), the server responds with a multipart mixed-replace content type header, instead of the normal MIME type for the file, as follows:

Content-Type: multipart/x-mixed-replace;boundary=foo

(In reality, a much longer boundary should be used, to avoid the possibility that one of the files served as data could contain the boundary, written on its own line after a blank line, with two dashes before it.)

The server never closes the connection, always pretending it has not finished sending the content to the browser. Whenever the server has some data to send to the browser, it sends the boundary, preceded by two dashes, with a blank line before it. It then sends the correct content-type header, followed by a blank line, followed by the data for that frame. Apart from the MIME type of the initial header, this is the same approach as used by MIME email to include attachments.

Content-Type: multipart/x-mixed-replace;boundary=foo

--foo
Content-Type: image/jpeg

... some data ...

--foo
Content-Type: image/jpeg

... some more data ...

Since the server never properly ends the content, the browser remains connected to it, waiting for more data to arrive. Each time the browser receives the data for a frame, it uses that to replace the previously displayed content.

This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.