FirebaseExtended / polymerfire

Polymer Web Components for Firebase
https://www.webcomponents.org/element/firebase/polymerfire
MIT License
459 stars 142 forks source link

Implementing Offline Write Data #115

Open justjoolz opened 8 years ago

justjoolz commented 8 years ago

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?

cdata commented 8 years ago

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.

justjoolz commented 8 years ago

@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?

Wuntenn commented 7 years ago

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

jshortall commented 7 years ago

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

pumpkin-codes commented 7 years ago

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

jshortall commented 7 years ago

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.

justjoolz commented 7 years ago

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.

tjmonsi commented 7 years ago

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