Rectangle 27 0

angularjs How to sync offline database with Firebase when device is online?

new Firebase('URL/.info/connected').on('value', function(ss) {
   if( ss.val() === null ) /* firebase disconnected */
   else /* firebase reconnected */
  • keep the local data simple and small
  • use set() to save the data into Firebase at the appropriate path
  • when the device comes online, convert the locally stored data to JSON

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).

Any solutions to dealing with Firebase authentication? Caching the tokens locally and allowing login without hanging the application?

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

Hi, I dont understando what do you mean with ... you can "prime" Firebase by ...Thx

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.

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.

Thanks for the tips, i'll quickly try to implement this. Next thing to do would be to be able to start offline...

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.

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

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?