|
Pushlets - Whitepaper
|
So what are these Pushlets and how do they work ? In its basic form a Pushlet is luckily extremely simple. Through a few examples I will show the basics. It is by now also time for some code !
Pushlets are based on HTTP streaming, a technique that is sometimes used in multimedia viewing applications such as QuickTime. Instead of closing the HTTP connection after fetching an HTML page, the connection is kept open while fresh data is pushed to the client.
Taking the idea of HTTP streaming we could develop a JSP (since that it easier to deploy, but it could be a servlet as well) that continuously sends new HTML content back to the client in a timer loop.
|
Click example 1 on the examples/basics page. This is not very useful since the pushed content is continuously appended to the page while we would like to refresh it.
Here we jump right into the Pushlet mechanics. Click on example 2 on the examples/basics page and see that the page is refreshed every 3 seconds. How the ... is this done ?
This example consists of three files: push-js-stream.html, push-js-stream-pusher.jsp and push-js-stream-display.html. push-js-stream.html is the main page which contains each of the two other files in HTML FRAMEs. Let's just follow the route of the events.
The following lists push-js-stream-pusher.jsp. This is a JavaServer page that is executed on the server when requested. The main body of this file is listed below.
|
NB there may be a problem with examples 1 and 2 when using JSPs: some servlet engines will "eat" the IOException when a client leaves such that the JSP page will never catch the exception. In that case the loop may run forever. This is one of the reasons that the Pushlet implementation uses a Servlet (where the IOException can be and is caught).
Again we see a timer loop which prints (line 21) some HTML to the browser every three seconds. But wait, it is not pushing HTML but JavaScript ! What does this mean ? Effectively it pushes a line like for example <script language=JavaScript >parent.push('Page 4')</script>. What does this mean for the browser ? The browser has its JavaScript engine running and obediently executes each next line coming in. It is calling a JavaScript function parent.push(). Now the parent is the parent of the FRAME it is in, which is our first file push-js-stream.html. Let's see what happens there.
|
We see the push() function called from within the JSP frame (pushletFrame) is writing whatever it gets passed in its argument 'content' into the displayFrame. This is a piece of Dynamic HTML: you can refresh the content of a frame or window by calling 'writeln' of its 'document' object. So the displayFrame is the real View where the content is displayed. It is initially black and displaying a 'Wait...' text until the first content is pushed from the server.
|
This is basically the whole idea of Pushlets: we just stream in lines of JavaScript from a Servlet (or JSP for the example). These lines get interpreted by the browser who may do something interesting. So effectively we have a callback from Java in the server to JavaScript in the browser client ! Phew, that was easy !
This example showed the mechanics, but there are still a couple of issues to be solved and features to be added. For this reason I've built a small server-side Pushlet framework shown in the class diagram, plus some JavaScript libraries for the client. Since the client heavily will rely on more DHTML features such as Layers we will walk through some DHTML first. See examples/dthml.
|
Pushlets - Whitepaper
|