Home > Software design >  Firestore: Stream change when query answers change
Firestore: Stream change when query answers change

Time:01-18

Id like some clarification onhow streams work. I have a users collection, where eveyone has a votes field stores as an int. I would like to make a stream listening to the top 5 users in terms of votes. Here's my questions:

  1. If on initial load, say the top 5 users have vote values of 6,5,4,3,1. Lets say a new user joins the app and gets two votes, beating the user with 1 vote. Will this be reflected in the stream?
  2. there are many users, but i am only interested in listening to the top 5. When a user not in the top 5 has their votes changed, will I be charged a read?

Thanks!

CodePudding user response:

tl;dr:

  1. Yes
  2. No

Longer explanation:

  1. If you use a realtime listeners and a user joins the top 5 that wasn't in it before, you will get a new event with the new, current top 5 documents. You'll also get a delta snapshot that flags the changes, where the user that is new in the top 5 is marked as added, while the user that left the top 5 is marked as deleted.
  2. Firestore charges document reads for documents that are read for you on the server. Documents that are not in the top 5 don't need to be read, so there is no charge for those.
  • Related