NAME HTTP.Push - Server Push via iframes and DOM events SYNOPSIS function pushHandler(text) { alert("Received some text: " + text); } var stream = new HTTP.Push({"uri" : "/cgi_bin/push.html", "onPush" : pushHandler}); stream.start(); DESCRIPTION Netscape Navigator 1.1 introduced the ability to perform a "server push" via an HTTP content type of multipart/x-mixed-replace. A long-running CGI would maintain an open TCP connection to the browser, sending new content at arbitrary intervals, without a new client request. This technique had some interesting applications, but was ahead of its time, and was rarely used. Support for multipart/x-mixed-replace was never added to Internet Explorer, so when Netscape lost the browser wars the technique was mostly abandoned. The recent AJAX and "rich Internet application" hype has breathed new life into old ideas. It is not uncommon to see attempts at creating browser-based IRC clients, IM clients, real-time log viewers, and other applications which could benefit from the streaming functionality provided by Netscape's server push. Unfortunately, since traditional server push is not supported in IE, most of these applications resort to polling. Every few seconds, a web-based IRC client will make a new HTTP request to the server (generally via XMLHttpRequest) to ask for new data. The shorter the polling interval, the quicker the client will receive an incoming message after it is produced, but the higher the CPU and bandwidth load on the server. Enter HTTP.Push. This module provides a cross-browser mechanism for performing server push using a long-running CGI, an iframe, and DOM elements. When a push is started, an invisible iframe is created and pointed to the URI of a long-running CGI. This CGI sends back data whenever it chooses, wrapped in HTML tags, inside the page's body. It does not matter which tags are used, nor what the contents of the tags are, so long as the iframe's DOM is updated. A setInterval() loop continuously polls the iframe for new DOM elements belonging to the body node (every 100ms by default). If a child node is found, the onPush callback function is called with that node as its argument. After the callback terminates, the node is deleted from the iframe's DOM, making room for more elements. An HTML document being pushed from the server should look something like this: Title! [ Whitespace padding goes anywhere before the body tag ] [ No whitespace between 'body' and the first node. ] [ There can be arbitrary delays between sending these elements: ]

74

[delay]

8

[delay]

47

[ ... ] In some browsers, streaming will not begin until the HTML document reaches a certain size. Whitespace padding (or some other type of padding) must be inserted, to compensate. Total HTML document size before the first data element (the first 'p' tag in the example above) should be at least 256 bytes to support Internet Explorer, or at least 1024 bytes to support Safari. Constructor var stream = new HTTP.Push({"uri" : "/cgi_bin/push.html", "onPush" : pushHandler, "interval": 100}); Create a new "HTTP.Push" object. "uri" (required) is the location of the CGI starting the push. "onPush" (required) is the handler to be called when new data arrives from the server. An "onPush" handler takes one argument, the DOM element sent in any given push. "interval" (optional, defaults to 100) is the interval between polls on the child iframe, in milliseconds. The average latency of processing a push will be one half of the interval, if pushes occur randomly. Methods start stream.start(); Start a push from the server. No data is transferred before "start" is called. EXAMPLES The "/src/f/fu/fuzzynerd/HTTPPush_004/README/push.html" and "push.html" files packaged with HTTP.Push show examples of server CGI implementation and HTTP.Push usage, respectively. SEE ALSO http://wp.netscape.com/assist/net_sites/pushpull.html -- Netscape server push documentation AUTHOR Larry Lansing COPYRIGHT HTTP.Push is copyright (c) 2005 Lawrence S. Lansing and is released under the MIT License: