8

I'm currently using angularJS and phonegap to build a test application for Android / iOS.

The app use only text data stored in a Firebase database. I want the app to have its own local database (used when the device is offline) and sometime (when the device is online) sync with a Firebase database.

The offline mode uses the storage API of phonegap/cordova. Could I just check the device's online state and backup the online database periodically ?

Any clues on how I can achieve this ? Last time a similar question was asked, the answer was "not yet"... (here)... because it focused on a hypothetical Firebase feature.

Community
  • 1
  • 1
Julien Tanay
  • 1,214
  • 2
  • 14
  • 20

2 Answers2

13

If Firebase is online at the start and loses its connection temporarily, then reconnects later, it will sync the local data then. So in many cases, once Firebase is online, you can simply keep pushing to Firebase during an outage.

For true offline usage, you will probably want to monitor the device's state, and also watch .info/connected to know when Firebase connects.

new Firebase('URL/.info/connected').on('value', function(ss) {
   if( ss.val() === null ) /* firebase disconnected */
   else /* firebase reconnected */
});

The way to achieve this with the current Firebase toolset, until it supports true offline storage, would

  1. keep the local data simple and small
  2. when the device comes online, convert the locally stored data to JSON
  3. use set() to save the data into Firebase at the appropriate path

Additionally, if the app loads while the device is offline, for some reason, you can "prime" Firebase by calling set() to "initialize" the data. Then you can use Firebase as normal (just as if it were online) until it comes online at some point in the future (you would also want to store your local copy to handle the case where it never does).

Obviously, the simpler the better. Concurrent modifications, limits of local storage size, and many other factors will quickly accumulate to make any offline storage solution complex and time consuming.

Kato
  • 40,352
  • 6
  • 119
  • 149
  • Thanks for the tips, i'll quickly try to implement this. Next thing to do would be to be able to start offline... – Julien Tanay May 30 '13 at 09:39
  • Hi, I don´t understando what do you mean with ... you can "prime" Firebase by ...Thx – david Apr 10 '14 at 21:07
  • Any solutions to dealing with Firebase authentication? Caching the tokens locally and allowing login without hanging the application? – MrYellow Dec 11 '14 at 22:27
  • The latest version of Firebase provides some improved support for auth in offline modes. Read about it in the docs. However, full offline support won't be possible until offline persistence is available. – Kato Dec 15 '14 at 17:24
  • What happens when the data is off time wise? If I set() or update() and it reconnects wont it consider that data as newer and overwrite the server? – Dennis Smolek Apr 05 '15 at 23:24
  • Yes, that's correct. There's a world of difference between push() or update()/set() and that's an entirely different question. It's been covered before here or on the mailing list and the solution is to use security rules. – Kato Apr 06 '15 at 00:49
  • @Kato do you have a link to the mailing list? I'm having a hard time finding much beyond the outdated OfflineFirebase to working examples. Our solution does use security rules in that our app has a generated read only user, when the system reconnects all of its "updates" are rejected but this feels hacky. I wish we could pass a flag to not push to the server or set a timestamp to the update call – Dennis Smolek May 12 '15 at 04:35
2

After some time, I would like to add $0.03 to @Kato's answer:

Opt to call snapshot.exists() instead of calling snapshot.val() === null. As the documentation points out, exists() is slightly more efficient than comparing snapshot.val() to null.

And if you want to update data prefer to use the update() method rather then set(), as the last will overwrite your Firebase data. You can read more here.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
yuriploc
  • 315
  • 3
  • 13