Is there an isOnline function in Google Gears to check whether the application is online? Do you plan to have one?

No, there isn't an isOnline function in Google Gears at this time. And currently, there are no plans to implement it.

There are 2 reasons why we haven't implemented it yet:

  1. There are multiple definitions of the application being 'offline'. The reason could be any of these:
    • The browser has the 'work offline' flag set
    • The network cable is unplugged
    • The VPN is down
    • DNS is down
    • The server you are trying to reach is down
    • The server has a bug and cannot process your request
    We can't assume how an application should determine if it is offline or not because each application will have different requirements.
  2. We don't want users to neglect syncing reliability.

    The popular suggestion to implement isOnline has been to make a periodic XMLHttpRequest to a resource, whose success/failure would designate the application being on/offline. The problem with this is that you cannot assume that when a ping succeeds you can start syncing and be guaranteed that that you are online. For example, if isOnline() returns true and you start sending sync updates, something could happen in the connection between the user and the server and these syncs would be permanently lost - meanwhile the application still thinks it is online.

    So instead what we encourage people to do is to queue all updates locally in the database module, and then try and send them to their server separately. If the update succeeds, remove them from the database. If the update fails for any reason, try again next time. In that way, you're always ready for the server to go down at any time and you never lose anything.

Sample Code for periodic XMLHttpRequests - uses Gears v0.2+ HTTPRequest

Here is some sample code that makes use of Gears' HTTPRequest module to change the UI of an application - let the user know when they are on/offline. It requires gears_init.js to be in the same directory.

<div id="serverStatus"></div>
<div id="pings"></div>
<script src="gears_init.js"></script>
<script type="text/javascript" charset="utf-8">
var numPings = 0;
var request = google.gears.factory.create('beta.httprequest', '1.0');
var TIME_BETWEEN_PINGS = 3*1000;
var PING_TIMEOUT_SECONDS = 1*1000;


function pingSuccess() {
	if(request.responseText != "" && 
		 request.responseText.indexOf("404 Page not found") == -1){
		document.getElementById('serverStatus').innerHTML = "[Server Accessible]";
	} else {
		document.getElementById('serverStatus').innerHTML = "[Server Inaccessible]";
	}
}

function isServerAvailable() {
	var resource_to_test = "FAQs_to_write.txt";
	resource_to_test += "?q="+Math.floor(Math.random() * 100000);
	request.open('GET', resource_to_test);
	window.setTimeout("pingSuccess()",PING_TIMEOUT_SECONDS);
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			numPings++;
			document.getElementById('pings').innerHTML = "Number of pings: " +
			                                              numPings;
		}
	};
	request.send();
	window.setTimeout("isServerAvailable()",TIME_BETWEEN_PINGS);
}

isServerAvailable();
</script>
	
Be aware that the resource_to_test must be a file located on the same domain this code is running on. Also note that in the pingSuccess function, there is a test for a page returning "404 Page not found". Different domains will send the user a different 404 error, so please be aware of what check is correct for your domain.

Sample Code for browser being offline

To check if the user has manually told the browser to work offline, use this:

alert(navigator.onLine);
	

Note: This works on IE and Firefox

Google apps
Main menu
13057748106032268205
true
Search Help Center
true
true
true
false
false