Does firebase web client download data every time I subscribe for a certain database path?
Does firebase web client download data every time I subscribe for a certain database path?
In my angular app, I use AngularFire
to access firebase realtime database.
AngularFire
I have following method to retrieve entities in a certain database path.
private db: AngularFireDatabase;
public getAll(): Observable<Item>
return this.db.list<Item>('some/db/path').valueChanges();
If I call above method multiple times, does the data get downloaded multiple times from server, or firebase client uses a local cache second time onward?
If data gets downloaded multiple times, is there any way I can instruct firebase client to use local caching?
1 Answer
1
The Firebase Realtime Database client deduplicates listeners. That means that:
this.db.list<Item>('some/db/path').valueChanges();
this.db.list<Item>('some/db/path').valueChanges();
The above code will only download the data once initially, and only download the delta once for each change.
Note that you can check this for yourself by looking at the Web Socket traffic in the network panel of your browser.
It does a so-called delta-sync in that case. This means it calculates the hashes of data it has cached, and sends those to the server. The server then only sends data that was modified back. This approach is not guaranteed to be minimal (that would mean the client would have to send a hash of each value), but is typically a lot smaller than sending all data.
– Frank van Puffelen
Sep 4 '18 at 15:06
Good to hear that. Also, I read in several places that if I use
keepSynced()
for a certain path in an android client, firebase will download all data of that path in each app restart. But, according to this mechanism you described, there shouldn't be any need to do so. Am I correct?– Lahiru Chandima
Sep 4 '18 at 15:14
keepSynced()
Correct. You can check the debug logs to see what actually happens on the wire. Especially the hashing is quite education, although a bit hard to follow.
– Frank van Puffelen
Sep 4 '18 at 16:54
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks for the answer. Not directly related to this, but I have another doubt. If I have persistence enabled in an android client, if the data of a certain db path is already cached, When the android app is restarted, if I subscribe for value changes of that path, will firebase download all data of that path again, or will it only download the diff between the cache and the server?
– Lahiru Chandima
Sep 4 '18 at 14:59