Offline/Background Sync?

613 views
Skip to first unread message

Gavin Doughtie

unread,
Jul 1, 2015, 5:39:22 PM7/1/15
to fireba...@googlegroups.com
What's the latest thinking on using Firebase when the page isn't running? I'm thinking particularly of the mobile/offline/browser killed state. Can Firebase run inside a Service Worker?


Michael Lehenbauer

unread,
Jul 1, 2015, 5:52:43 PM7/1/15
to fireba...@googlegroups.com
Hey Gavin,

Currently Firebase is target at synchronizing data while the app is running.  We unfortunately don't have any background sync capabilities.  For mobile, you could send a push notification to wake the app up and it would "catch up" (sync the latest data) once it's started (assuming you're online), but I know this doesn't replace the need for traditional background sync.

Not sure if anybody's experimented with Firebase from a service worker.  In theory, I think it should work, since we only rely on WebSockets.  But if you try it and run into issues, let us know!  Else somebody from the community may be able to chime in.

Best regards,
-Michael



On Tue, Jun 30, 2015 at 1:53 PM, Gavin Doughtie <gavin.d...@gmail.com> wrote:
What's the latest thinking on using Firebase when the page isn't running? I'm thinking particularly of the mobile/offline/browser killed state. Can Firebase run inside a Service Worker?


--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/61236690-5fe8-4de9-af97-e4f9337f3e63%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rob Becker

unread,
Aug 24, 2015, 4:47:00 PM8/24/15
to Firebase Google Group
Hi Gavin,  

I tried with version 2.2.4 from the CDN and was experiencing console errors with firebase trying to access document (DOM isn't accessible from a service worker). 
But, good news! I'm using 2.2.9 from a service worker and it seems to work. I can even close the tab, and still get updates from firebase and show a HTML5 notification when needed.

This is a pretty easy version of push notifications for the web!

Rob Becker

unread,
Aug 24, 2015, 6:35:07 PM8/24/15
to Firebase Google Group
I jumped the gun a bit.  It works as long as the service worker is active .... but once it is halted after you close the tab, it doesn't work. So, to wake the service worker, you'll still need some sort of push messaging solution. Google Cloud Message is an obvious choice, but there's other's like Parse and Pushover.

Rob Becker

unread,
Aug 26, 2015, 12:42:17 AM8/26/15
to Firebase Google Group
One more update, you can get "push style" notifications by keeping the service worker alive. All in all it probably isn't a great idea to keep the service worker alive forever, but it works. In my case I have a few minutes where I want to keep the service worker alive while I wait for a server side job to finish. So, I keep the service worker awake until it is finished, and then show a notification on completion.

You can do this hack by:
1) making a fake network request that will be intercepted by the service worker (say /zombie)
2) in the service worker, in your event handler for "fetch" you can check if it is your zombie request, and "event.waitUntil" a promise that you never resolve. (or resolve after a set amount of time or whatever)

The code in service-worker.js looks like this:

self.addEventListener('fetch', function(event) {
    console.log('fetching', event.request.url);
    if (String(event.request.url).indexOf('zombie') >= 0) {

        var forever = new Promise(function(resolve, reject) {
            console.log('Never gonna resolve this promise');
        });
        // Hack to keep the service worker alive ... forever!
        event.waitUntil(forever);
        event.respondWith(forever);
    } else {
        event.respondWith(fetch(event.request));
    }
});

Vishwanath Arondekar

unread,
Nov 14, 2015, 10:53:49 AM11/14/15
to Firebase Google Group
I tried the given code for keeping service worker alive with waitUntil. I found out that even after this, service worker is terminated by browser after some time. 

You might want to check this.


The lifetime of a service worker is tied to the execution lifetime of events and not references held by service worker clients to the ServiceWorker object.
A user agent may terminate service workers at any time it:
  • Has no event to handle.
  • Detects abnormal operation: such as infinite loops and tasks exceeding imposed time limits (if any) while handling the events.
Reply all
Reply to author
Forward
0 new messages