Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase offline cache & original firebase.js source code

My question is a follow-up to this topic. I love the simplicity and performance of Firebase from what I have seen so far.

As I understand, firebase.js syncs data snapshots from the server into an object in Javascript memory. However there is currently no functionality to cache this data to disk.

As a result:

  1. Applications are required to have a connection when they start-up, thus there is no true offline access.
  2. Bandwidth is wasted every time an app starts up by re-transmitting all previous data.

Since the snapshot data is sitting in memory as a Javascript object, it should be quite trivial to serialize it as JSON and save it to localStorage, so the exact application state can be loaded next time the app is started, online or not. But as the firebase.js code is minified and cryptic I have no idea where to look.

PouchDB handles this very well on a CouchDB backend. (But it lacks the quick response time and simplicity of Firebase.)

So my questions are:

1. What data would I need to serialize to save a snapshot to localStorage? How can I then load this back into Firebase when the app starts?

2. Where can I download the original non-minified dev source code for firebase.js?

(By the way, two features that would help Firebase blow the competition out of the water: offline caching and map reduce.)

like image 1000
Colin Skow Avatar asked Nov 08 '13 06:11

Colin Skow


People also ask

Is it possible to use Firebase offline?

By enabling persistence, any data that the Firebase Realtime Database client would sync while online persists to disk and is available offline, even when the user or operating system restarts the app. This means your app works as it would online by using the local data stored in the cache.

Does Firebase have a cache?

Firebase Hosting uses a powerful global CDN to make your site as fast as possible. Any requested static content is automatically cached on the CDN. If you redeploy your site's content, Firebase Hosting automatically clears all your cached static content across the CDN until the next request.

Is Firebase persistent?

Note that Firebase Auth web sessions are single host origin and will be persisted for a single domain only. Indicates that the state will only persist in the current session or tab, and will be cleared when the tab or window in which the user authenticated is closed.

Does Firebase have unlimited Storage?

Understand Hosting storage These files make up your project's Hosting storage usage level. This Hosting storage is independent and unrelated to any other storage for your Firebase project (like Cloud Storage for Firebase or database storage). Note that Hosting has a maximum size limit of 2 GB for individual files.


1 Answers

Offline caching and map reduce-like functionality are both in development. The firebase.js source is available here for dev and debugging.

You can serialize a snapshot locally using exportVal to preserve all priority data. If you aren't using priorities, a simple value will do:

var fb = new Firebase(URL);
fb.once('value', function(snapshot) {
   console.log('values with priorities', snapshot.exportVal());

   console.log('values without priorities', snapshot.val());
});

Later, if Firebase is offline (use .info/connected to help determine this) when your app is loaded, you can call .set() to put that data back into the local Firebase. When/if Firebase comes online, it will be synced.

However, this is truly only suitable for static data that only one person will access and change. Consider, for example, the fallout if I download the data, keep it locally for a week, and it's modified by several other users during that time, then I load my app offline, make one minor change, and then come online. My stale changes would blow away all the work done in between.

There are lots of ways to deal with this--conflict resolution, using security rules and update counters/timestamps to detect stale data and prevent regressions--but this isn't a simple affair and needs deep consideration before you head down this route.

like image 180
Kato Avatar answered Sep 24 '22 06:09

Kato