Thursday, October 25, 2007

Offline Web Applications

That doesn't sound like it makes sense, does it? Why would you want to use a web application without the internet? Well, it makes sense for some things. Wouldn't it be nice to access your GMail account when your stuck at the airport and don't feel like paying for wifi access? A lot of web applications nowadays are used to store user data. The user has to log on to access it; there is nothing on the client. When the users wants access to this data when there is no net connection, we have a problem. So what do you need to build an offline web application?

For one, you need storage on the client. Remember, web applications run in the browser. The browser has only limited access to a client's machine. They can't just write massive amounts of data to the hard disk. The first requirement then, is local storage. Figuring out what to keep cached on the client is a whole other story.

Next, you need some way for the client to access your web page without accessing the internet. You need some way to cache the page(s) for the client's browser.

Lastly, your web application needs to act as though it is communicating with the server when it really isn't. You need some application logic to be stored on the client as well.

Sounds like its pretty complicated, but there are some solutions out there.

Google Gears is probably the most popular. It is still in an early developer beta though.
There is Dojo Offline Toolkit, which is nearing version 1.0.
Web2OS looks pretty interesting as well, but it is still in a private beta.
The WHATWG is also trying to come up with some standards to support these ideas.

How about rolling your own solution? Lets see how to provide the requirements for an offline web app using Java.

Technologies:
Applets, LiveConnect:
Allows javascript to communicate with Java Applets. Derby: An embedded Java database

First, the web application has a hidden applet (1px by 1px) that is loaded in the background. The applet is cached locally on the client's machine. This allows them to use it offline or online. When the applet starts up, it starts the embedded database which can store data on the client, even across sessions. They can close the browser, shut the computer down, and it'll still be there when they get back. This solves the local storage problem. Now for application logic. With LiveConnect, the javascript can call Java methods in the applet. So instead of directly communicating with the server, the client code communicates with the applet (which is also available offline). The applet can then check for a net connection and perform the appropriate action, either forward the request to the real server or do something locally. We'd have to rely on the browser's caching ability to cache the applet and javascript/HTML pages.

I suppose that may work in theory, but actually getting it to work in practice may be another story (I can image Applet security issues, browser caching issues, just to name a couple problems). Just some thoughts.
Check out Francois Orsini's presentation, Enabling Offline Web Applications with Java DB, from JavaOne 2007 for more details. He also mentions possibly embedding an HTTP server, like Jetty, in the Applet.

No comments: