polymerfire icon indicating copy to clipboard operation
polymerfire copied to clipboard

Implementing Offline Write Data

Open justjoolz opened this issue 8 years ago • 8 comments

What's the recommended approach for implementing offline write?

I've got firebase-query syncing with app-storage/indexeddb-mirror but not sure the best approach to storing data locally and both updating the local firebase elements when offline, and the database when connectivity is regained.

In this video a Codelab is mentioned but not linked: https://youtu.be/SobXoh4rb58?t=32m27s

Any pointers on how to get this setup?

justjoolz avatar Sep 14 '16 04:09 justjoolz

app-indexeddb-mirror is designed to work within the constraints of Firebase's available features. So, it allows for mirroring live data for offline-read, but it does not enable synchronization or conflict resolution features for Firebase.

Firebase currently lacks built-in support for offline-to-online synchronization and conflict resolution. So, you must first design your own conventions for synchronization and conflict resolution. If you are willing to design your own synchronization and conflict resolution schemes (or if you don't care that a client returning online will always blindly overwrite values in Firebase), then one reasonable strategy is:

  1. Observe changes to a data object that should be synchronized with Firebase.
  2. When offline, cache a serialized version of each such change in a local storage layer such as localStorage or IndexedDB.
  3. Re-play all recorded changes when the user returns online so that Firebase attempts to save it.
  4. Remove the cached, serialized version of each change as it successfully saves to Firebase.

Please note that without any conventions for synchronization and conflict resolution, the above approach can be problematic when applied to data that multiple users might be editing.

If you are interested in a storage layer with built-in synchronization and conflict resolution features, I would personally recommend PouchDB.

cdata avatar Sep 14 '16 04:09 cdata

@cdata Thanks for you answer.

This particular use case doesn't require any conflict resolution as a user only has access to their own private data.

I'm going to go ahead and try swapping out app-indexeddb-mirror for app-pouchdb and abstracting all firebase behind pouchdb, is that what you recommend? So pouchDB becomes the main db and firebase acts more as a backup of that data.

@mbleigh is this the technique used in the Codelab link mentioned in your talk? Also do you have that link? Or any basic examples of offline write with polymerfire?

justjoolz avatar Sep 14 '16 08:09 justjoolz

@justjoolz I was thinking about this also, I think you may still have to anticipate conflicts.

One user can have many devices and for example may decide to continue work on a second device which has connection.

When the original (offline) device regains connection it would synchronise and potentially obliterate the data that was saved by the connected second device in the interim.

Wuntenn avatar Oct 24 '16 19:10 Wuntenn

@justjoolz Did you ever find the offline-write codelab mentioned by @mbleigh in that I/O Firebase talk?

Or did you have any success implementing this with pouchdb? I'm interested in doing this too.

jshortall avatar Nov 11 '16 14:11 jshortall

@jshortall did you impliment the pouchdb with firebase iam also intrested

pumpkin-codes avatar Aug 24 '17 10:08 pumpkin-codes

No, due to the complexity to build, that feature was back-burnered for now. I am hoping that one day soon @mbleigh will just announce Firebase full support for offline-first apps!

I am also just looking into rolling with PouchDB/CouchDB for another app that must be offline.

jshortall avatar Aug 28 '17 15:08 jshortall

I did setup a pouchdb first implementation that synchronized with firebase too. I haven't touched that codebase since last year though. Wasn't too tricky in my use case.

justjoolz avatar Aug 29 '17 03:08 justjoolz

I did use PouchDB and it was working great. But I used my own implementation of a firebase property mixin with PouchDB for it

tjmonsi avatar Sep 04 '17 00:09 tjmonsi