Ad

How To Only Subscribe To Firebase Collection Changes

I'm using the firebase javascript sdk via @angular/fire in an angular app.

Using the angularfire Collection#stateChanges(), I thought I was just subscribing to the state changes of a collection (based on the description). I.e. on initial subscribe nothing would be returned. However, I'm finding that stateChanges() immediately returns everything in the collection before then continuing to return state changes.

Is there a way to just subscribe to future state changes and not "replay" past changes (either using the firebase-js-sdk directly or via @angular/fire)?

For example, if you already had a copy of the current state of a collection downloaded, you would not want to re-download the collection state when simply subscribing to collection updates.

Thanks for any help!

Ad

Answer

At the moment, firebase does not support only subscribing to query changes. When you subscribe to a query, you must also download all of the related documents immediately (see issue #1551 in the firebase-js-sdk repo).

This being said, as pointed out in that issue, you can fake this functionality by adding an updatedAt property to your documents and querying for all documents where updatedAt > now. This would subscribe you to just future changes.

A limitation of this work around is that Firestore only supports using an inequality filter (>=,<, etc) on a single field in a query, so using your inequality filter up on the updatedAt field may be problematic.

Ad
source: stackoverflow.com
Ad