Detecting Online / Offline

2 views
Skip to first unread message

bigjoepo

unread,
Jun 1, 2007, 3:06:15 PM6/1/07
to Google Gears
First off, I LOVE Google Gears. It's exactly what I needed. I just
have one question - is it possible to detect when the user switches
from online to offline?

You mention that there's 2 ways to synchronize: Constantly try and
synchronize, or allow the user to synchronize (thus saying "im going
offline").

Is the only way to detect that the user has gone offline in the middle
of using the application to constantly be doing XHR requests for
synchronization, and set timers to check if they come back in time?

Thanks
Joe Potenza

shess

unread,
Jun 1, 2007, 4:48:19 PM6/1/07
to Google Gears
Except for when the browser is explicitely in offline mode, you just
can't tell if you're online. You could be online in the connected-to-
the-Internet sense, but the server could be unreachable. You could be
online in the connected-to-the-LAN sense, but the Internet could be
unreachable. Two requests sent a couple seconds apart could have one
get a response and the other not, and varying orders. So the only
real option is to keep pinging.

In practice, the user _generally_ are going to be either online or
offline, transitions aren't that frequent. It's just that the only
way to know at any given time is to try.

-scott

Shakakai

unread,
Jun 1, 2007, 5:06:03 PM6/1/07
to Google Gears
I haven't seen a documented example of the Gear API providing that
functionality. You could use a WorkerPool thread to send an XHR
request to google.com. If it succeeds, you know there's a connection.

That was actually the approach I used for an early Apollo application
before they added an API to handle connectivity.

This does create a bit of a race situation whereby the user could
disconnect right after you recieve the Google request back. That won't
happen very often though.

-Todd

bigjoepo

unread,
Jun 1, 2007, 5:51:08 PM6/1/07
to Google Gears
Scott,
Thanks for your response. So is there a way to tell if the browser
is explicitely in offline mode? Just being able to detect that much
would be great.

On Jun 1, 4:48 pm, shess <Scott.H...@gmail.com> wrote:

Scott Hess

unread,
Jun 1, 2007, 7:07:08 PM6/1/07
to google...@googlegroups.com
It looks like Firefox 2.0 exposes navigator.onLine, but you need 3.0
to get the ononline and onoffline events. See also
http://www.whatwg.org/specs/web-apps/current-work/#offline .

-scott

Reply all
Reply to author
Forward
0 new messages