Why Do You Need Gears?

Google Gears (officially called just Gears) is Google’s open source software that enables offline access to normally web-based services. When you incorporate Gears into your application or gadget code, that application can function when the user is not connected to the Internet—effectively turning your web-based application into traditional desktop software.

Why is that important?

Imagine, for example, that you’re working on a big report using Google Docs, Google’s web-based word processor. All is well and good until you head off to your in-laws’ house for the weekend. The in-laws, unfortunately, don’t have an Internet connection in the house, and you can’t sneak away to use the WiFi service in the local Starbucks. So, while you could be working on your report during your downtime, you’re instead stuck with a computer that can’t access either your application or your document because it isn’t connected to the Internet.

In this instance, if you could run your application offline and if your document was stored on your own computer, you could keep working and get your report done on schedule. Well, that’s what Google Gears does—lets you run web-based applications locally, with no Internet connection necessary.

As you might recall from Chapter 19, “Using Google Docs,” Google does offer offline workability for its web-based applications. This offline mode is facilitated by—you guessed it—Google Gears.

Now any application developer can utilize Gears to enable their own web-based applications to run locally without an Internet connection. If your application can benefit from this offline functionality, you need to incorporate Gears into your development plans.

How Does Google Gears Work?

A traditional web-based application works as shown in Figure 46.1. The application’s user interface, displayed within the user’s web browser, constantly accesses the Internet, where the application itself is stored. Separately, the application’s data—via the browser-based data layer—also has to access the Internet to store the data online.

Figure 46.1. The flowchart for a traditional web-based application. (Illustration courtesy Google.)

The flowchart for a traditional web-based application. (Illustration courtesy Google.)

Note

Not all web browsers support Google Gears. At present, Gears runs only on Internet Explorer 6 or higher and Firefox 1.5 or higher. Google Gears is also built into the new Google Chrome web browser.

Google Gears works in a slightly different manner. Key to its operation is the installation of a database engine on the user’s computer. This database locally caches the user’s data, which was previously stored exclusively on the Web.

This is accomplished via means of a data switch, as shown in Figure 46.2. This switch directs the data information to the local database when the user is offline, or to the web-based storage when the user is online. Gears also adds a Sync Engine that synchronizes the offline data with the online data.

Figure 46.2. The flowchart for a Gears-enabled application. (Illustration courtesy Google.)

The flowchart for a Gears-enabled application. (Illustration courtesy Google.)

When the user is offline, then, Gears-enabled applications use data from the local cache instead of from the web-based service. Thus, a user can run your application in offline mode, using the locally cached data; when he goes back online, the local data is synchronized with the version stored on the web.

To make this all work, Gears utilizes three core modules, each of which has its own API:

  • LocalServer, which caches and serves the application itself on the user’s computer, without needing to contact a web-based server.

  • Database, which stores and accesses the application’s data on the user’s computer.

  • WorkerPool, which makes the application more responsive by performing long-running tasks in the background—such as the code that synchronizes data between the web-based server and users’ computers.

Note

Gears uses the open source SQLite database system for its database.

Like all of Google’s developers tools, Google Gears is open source and free for all developers to use.

Developing Gears-Enabled Applications

To use Google Gears with your application, you have to add explicit Gears code into your application’s source code. In addition, all of your application’s users must have Gears installed on their computers.

Installing Gears on the Host Machine

It’s best if your application’s users already have Gears installed on their computers. One installation of Gears works with all Gears-enabled applications, so if they’ve already installed Gears to use Google Docs offline, for example, that installation of Gears is available for you to use with your application.

To install Gears, users should go to gears.google.com. When they click the install link on this page and follow the resulting instructions, the Gears application is installed on their computer.

Initializing Google Gears

When you integrate Gears into your own application, the first thing you want to do is determine whether the user has Gears installed. You do this by initializing Gears with the gears_init.js source file.

This file detects whether Gears is installed. If it is, it defines Gears for your application. If Gears isn’t installed, it directs the user to the Gears installation page.

You can download the gears_init.js source file from code.google.com/apis/gears/tools.html#gears_init. Then reference the file with the following code:

<script src="gears_init.js"></script>
<script>
  if (!window.google || !google.gears) {
    location.href =
   "http://gears.google.com/?action=install&message=<your welcome
message>"
     + "&return=<your website url>";
  }
</script>


Replace your welcome message with your own welcome message, such as “Please install Gears to enable offline operation of this application.” Replace your website url with the URL for your application’s website.

Developing for Gears

Now you have to add Gears functionality to your application’s source code. In particular, you need to add code for the following functions:

  • Use the LocalServer Module API to enable your application to cache and serve its HTTP resources locally, on the client computer.

  • Use the Database Module API to add local data storage to your application.

  • Use the WorkerPool Module API to run your application’s JavaScript code in the background.

With the appropriate code in place, your application will:

  • Sense the Internet connection and run in offline mode when no connection to the Internet is present.

  • Automatically store copies of the application’s data/documents locally, on the client computer’s hard drive.

  • When the offline application reconnects to the Internet, automatically synchronize any changes made in the local database with the web-based server.

To download the Gears APIs and learn more about developing for Gears, go to code.google.com/apis/gears.???

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.196.234