Jul 27
Digg
Stumbleupon
Technorati
Delicious

What Google Gears may offer to Symfony ?

Hi all,

I’m pretty sure you’ve already heard of Google Gears (now it’s just “Gears” as the other companies along with Google joined to the development process), so just to remind Gears is a browsers plug-in which makes possible your site works offline on users machines. This technology incudes 3 main core elements:

  • LocalServer Cache and serve application resources (HTML, JavaScript, images, etc.) locally
  • Database Store data locally in a fully-searchable relational database
  • WorkerPool Make your web applications more responsive by performing resource-intensive operations asynchronously

I’d mention at least 2 ways for how you can use this technology in your symfony application. Btw, I was trying to find anything about gears on symfony forum and it’s mostly unresponded stuff like this:

http://www.symfony-project.org/forum/index.php/m/34645/?srch=gears#msg_34645

So first way is a simple way which WordPress has chosen. You can simply add caching stuff to your application via using LocalServer component. This way is pretty simple you add standard initial code for gears

<?php use_javascript(‘gears_init’) ?>
<script>
if (!window.google || !google.gears) {
location.href = “http://gears.google.com/?action=install&message=” +
“&return=“;
}
</script>

and then use something similar to what Google proposes in samples:

// Change this to set the name of the managed resource store to create.
// You use the name with the createManagedStore, and removeManagedStore,
// and openManagedStore APIs. It isn’t visible to the user.
var STORE_NAME = “my_offline_docset”;

// Change this to set the URL of tha manifest file, which describe which
// URLs to capture. It can be relative to the current page, or an
// absolute URL.
var MANIFEST_FILENAME = “tutorial_manifest.json”;

var localServer;
var store;

// Called onload to initialize local server and store variables
function init() {
if (!window.google || !google.gears) {
textOut(“NOTE: You must install Gears first.”);
} else {
localServer = google.gears.factory.create(“beta.localserver”);
store = localServer.createManagedStore(STORE_NAME);
textOut(“Yeay, Gears is already installed.”);
}
}

// Create the managed resource store
function createStore() {
if (!window.google || !google.gears) {
alert(“You must install Gears first.”);
return;
}

store.manifestUrl = MANIFEST_FILENAME;
store.checkForUpdate();

var timerId = window.setInterval(function() {
// When the currentVersion property has a value, all of the resources
// listed in the manifest file for that version are captured. There is
// an open bug to surface this state change as an event.
if (store.currentVersion) {
window.clearInterval(timerId);
textOut(“The documents are now available offline.\n” +
“With your browser offline, load the document at ” +
“its normal online URL to see the locally stored ” +
“version. The version stored is: ” +
store.currentVersion);
} else if (store.updateStatus == 3) {
textOut(“Error: ” + store.lastErrorMessage);
}
}, 500);
}

// Remove the managed resource store.
function removeStore() {
if (!window.google || !google.gears) {
alert(“You must install Gears first.”);
return;
}

localServer.removeManagedStore(STORE_NAME);
textOut(“Done. The local store has been removed.” +
“You will now see online versions of the documents.”);
}

// Utility function to output some status text.
function textOut(s) {
var elm = document.getElementById(“textOut”);
while (elm.firstChild) {
elm.removeChild(elm.firstChild);
}
elm.appendChild(document.createTextNode(s));
}

where tutorial_manifest.json should contain information about cached files:

{
“betaManifestVersion”: 1,
“version”: “my_version_string”,
“entries”: [
{ “url”: “go_offline.html”},
{ “url”: “go_offline.js”},
{ “url”: “../gears_init.js”}
]
}

So this way even when user goes offline he still is able to work with cached pages.
As for me more interesting feature is Database module. This way along with cached pages you are able to allow user to store locally database (Gears uses SQLite for storing data). Here is a proposed sample:

<?php use_javascript(‘gears_init’) ?>
<script type=”text/javascript”>
var db = google.gears.factory.create(‘beta.database’);
db.open(‘database-test’);
db.execute(‘create table if not exists Test’ +
‘ (Phrase text, Timestamp int)’);
db.execute(‘insert into Test values (?, ?)’, [‘Monkey!’, new Date().getTime()]);
var rs = db.execute(‘select * from Test order by Timestamp desc’);

while (rs.isValidRow()) {
alert(rs.field(0) + ‘@’ + rs.field(1));
rs.next();
}
rs.close();
</script>

Disadvantage is you need to clone your mysql / pgsql stuff for sqlite but thanks God common SQL commands are mostly the same so it wont be too complicated.
Interesting if the other companies proposes similar mechanism to what Gears proposes. I’m sure making sites to work in online/offline mode still has big perspectives.


Author: symfonian

3 Comments

[…] What Google Gears may offer to Symfony ? […]

[…] What Google Gears may offer to Symfony ? | SymfonyLab […]

Justus Rambaldi
February 18, 2011

Very interesting the use of SQLite for Database Replication, it?s will be usefull for my actual project.
But my approach is reverse, my user need off-line web application installed inside the netbook used outside of company.
Later he connect with the intranet or remoting access and update the data on Server.

Comments RSS TrackBack Identifier URI

Leave a comment