0

Im using Firestore with web javascript sdk. Assume following scheme:

User Doc -> Friends collection

I want to know when someone change/remove/add data to it.

so what I wrote is something like this:

friendsCollectionRef.onSnapshot(snapshot => {
    snapshot.docChanges().forEach(change => {
        onChange(change);
    });
});

The problem is that whenever I refresh the page, it keeps calling the onChange with data that was updated in my last session.. Is there a way to get only NEW data and not retroactively?

I would like to avoid store "LastUpdate" field on everything. This, of course, should not be done in client side because then I pay for network which im never going to use.. So storing a boolean isFirstCall in out of the question.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Ori Refael
  • 2,888
  • 3
  • 37
  • 68

1 Answers1

1

As explained in the doc, when you listen to multiple documents in a collection with the onSnapshot() method:

The first query snapshot contains added events for all existing documents that match the query. This is because you're getting a set of changes that bring your query snapshot current with the initial state of the query.

So each time you refresh your page you are calling again the onSnapshot() method "from scratch" and therefore you get the first query snapshot with all the collection docs.

In other words, I think you will have to implement your "home-made" mechanism to only get the documents you want (probably a "LastUpdate" field...).

You may be interested by this SO answer which shows how to add a createdAt timestamp to a Firestore document via a Cloud Function. You could easily adapt it to record the last update. It would be more complicated if you want to detect the Documents that were deleted since the last fetch.

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
  • so basically everywhere i do "onSnapshot" i need to use ".where("createdAt", ">", new Date())" before? – Ori Refael Mar 24 '20 at 12:57
  • Well, this query will return zero document as it is querying for documents created in the future. It is difficult to say how you should exactly do, as we don't know what does "data that was updated in my last session" exactly mean. How do you define "last session"? You need to work out a way to save the date of the last session in the database and use this date to filter you query. You could be interested by the presence mechanism, see https://firebase.google.com/docs/firestore/solutions/presence – Renaud Tarnec Mar 24 '20 at 13:58
  • i think i wasn't clear enough. i meant i want to get all data which is new to me since i logged in. since im using 2 different database here, one is server's and the other one is strictly for live updates, i want user to only listen to live changes that occur while hes online. Any changes that happened before he became only doesnt really matter to him. so what i actually want here is that since the user logs in, i want to listen to live changes. so it means that i need to filter any event that occurred before the users logged in. right? – Ori Refael Mar 24 '20 at 14:57