Google Gears – A Client Programming Framework?

By admin - Last updated: Tuesday, October 7, 2008 - Save & Share - Leave a Comment

Google Gears beta is an extension to the browsers, it provides extra functionality for javascript applications to cache resources aka localserver, relational database (SQLite) and a worker thread pool.

localserver
Instead of remote server, this feature allows resources like html, javascript or images cached locally on desktop’s hard disk. This makes offline web application possible. It intercepts all the requests to the remote server, the localserver serves the requests when they have met the certain criteria which prevents old data.
Javascript Code Snippet

// initialze localServer object.
var localServer = google.gears.factory.create('beta.localserver');
// create a managed store which will do sync.
var store = localServer.createManagedStore('mylocalserver');
// set the manifest for resources to cache.
store.manifestUrl = 'manifest.json';
// ------------------------------
// manifest.json defined as JSON contains server resources
// that can be managed within the localserver store
{
  "betaManifestVersion": 1.0,
  "version": "1.0",
  "entries": [
    { "url": "index.html" },
    { "url": "test.js" },
    { "url": "logo.gif" },
 ]
}

There are two kinds of stores available to localserver, ResourceStore and ManagedResourceStore. ManagedResourceStore polls the network server to synchronize the local cache periodically. But the synchronization of ResourceStore has to be managed by the code itself by calling store.checkForUpdate();

Database
SQLite is utilized by Gears for persisting relational data. Web applications can perform CRUD to interact with the database within browser just like server side applications performs database operations.
Javascript Code Snippet

// initialize db connection.
var db = google.gears.factory.create('beta.database');
// set database to 'myDB' context
db.open('myDB');
// create table
db.execute('create table if not exists tblNews(content text, timestamp int)');
// insert.
db.execute('insert into tblNews values (?, ?)',
                  ['new here..', new Date().getTime()]);
var rs = db.execute('select content, timestamp from tblNews');
// iterate row set.
while (rs.isValidRow())
{
  alert(rs.field(0) + '@' + rs.field(1));
  rs.next();
}
// close row set.
rs.close();
// delete all.
var rs = db.execute('delete from tblNews');

Does this remind you writing server side script in your daily programming, prepare db connection, SQL statements, iterate the recordset, and close the connection.

Worker Pool
Javascript Code Snippet

// index.html
// initialize workpool object.
var workerPool = google.gears.factory.create('beta.workerpool');
// assign a delegate for worker's message event.
workerPool.onmessage = function(message)
{
  alert('Message from worker ' + message.sender + ': \n' + message.body);
};
// use target script as a worker.
var workerId= workerPool.createWorkerFromUrl('worker.js');
// send message to the worker.
// worker will execute asynchronously.
workerPool.sendMessage(999999, workerId);

// ------------------------------
// javascript in worker.js
// do some heavy tasks in this javascript
google.gears.workerPool.onmessage =
 function(msg)
 {
    google.gears.workerPool.sendMessage(
             longRunning(msg.body), msg.senderId);
 };

function longRunning(n) {
  var r= 0;
  while (n > 0)
  {
    n--;
    r+= 1;
    var tempString = 'a' + '1';
  }
  return r;
}

The features described above are just key components shipped from Gears. Apart from those, it also provides Timer utilities, GeoLocation service, Desktop Integration, AJAX and large binary data management. The final feature set is able to deliver a whole new framework for browser programming model. Since Google delivered its beta browser Chrome (very fast!), it is quite possible to embed Gears as part of the package in its formal release or even unify other open source frameworks like JQuery, prototype etc to establish the new open client side programming framework.




Posted in Web Application • Tags: , , , , , Top Of Page

Write a comment


Captcha: × 7 = twenty eight