Google recently announced its Gears project, explaining that it enables web applications to work offline.
The demonstration application is Google Reader, an online blog reader. If Gears is installed, you can click an icon to go offline, and it will download the last 2,000 blog items.
You can then return to that URL and read the entries, even if the PC is no longer online. This works by downloading the data to a local database, along with the pages and Javascript code that form the application.
Reader also keeps a record of which blog items are marked as read. When you reconnect, this information is synchronised back to the internet.
Gears is worth exploring even if offline capability is not required, since it adds new programming capabilities that can make any web application smoother and more responsive.
Gears itself is written in Javascript and C/C++; it is open-source code and available for download from http://code.google.com/apis/gears. It is a browser plug-in composed of three components, along with a Javascript library to enable their use, and runs on Windows, Mac and Linux. These are the components:
Localserver – Although this sounds like a local web server, it’s not really that smart. It is better to think of it as a cache for your application’s content and script. Browsers already cache content, but with Localserver you can ensure that all the pages required for your application to run are present, and in the same version, whereas normally you can’t be sure exactly what the browser chooses to cache.
Database – This is SQLite, a superb lightweight open-source database engine written in C. It is widely used in other projects, including PHP 5 and Apple’s Core Data. SQLite has been covered in this column before; the Gears version has been extended to include full text search.
Workerpool – This lets you execute Javascript in a background thread. The advantage is that long-running synchronisation operations run without locking up the user’s browser.
Google Gears is a low-level Javascript API. There is no magic that will transform an existing web application into one that will work offline, with just a few lines of code. Rather, it is down to the developer to write the code that will make it happen.
There are several possible approaches. Google’s Reader example has specific online and offline modes. This works but it’s not great. The ideal is to have an application that works the same whether online or offline, with transparent synchronisation when connected.
One idea is to write the application primarily for offline use, adding code for synchronisation. That implies major changes to existing code that might be in PHP, Java, ASP.Net, or some other language.
All the client-side Javascript code will be accessible to inquisitive users, so avoid including anything secret. The same applies to data that gets written locally. Not all applications are suitable for Gears. Online banking, for example, is a poor candidate, since the data is confidential and transactions cannot be conducted offline.
All OnlineTags: Visual Programming
