You can use a SnapshotListener
as a workaround, which allows us to listen for changes in real-time.
You will be charged for readings as if we had sent a new query if the listener is disconnected for more than 30 minutes. If the listener is disconnected every 31 minutes in the worst-case scenario, we will be charged 50 reads each time.
As a result, this technique is only practicable when the listener is not frequently disconnected.
According to the documentation, I found you can reduce the number of reads using get()
.
In each document in the collection, you must add a new property named lastModified
of type Date. When you create a new document or edit an existing one, you must use FieldValue.serverTimestamp() to set or update the field's value.
Reviewing Firebase documentation, I found that high read or write rates to lexicographically close documents need to be avoided to avoid contention faults in your application. Hotspotting is the term for this problem, and it can occur if your program does any of the following:
- Creates new documents at a rapid rate and assigns its own IDs that
are monotonically rising.
- A scatter algorithm is used by Cloud Firestore to assign document
IDs.
- If you use automated document IDs to create new documents, you must
not see hotspotting on writes.
- In a collection with few documents, creates new documents at a fast
rate.
- Creates new documents at a rapid rate with a monotonically growing
field, such as a timestamp.
- Deletes a large number of documents from a collection.
- Writes to the database at a rapid rate without growing traffic
gradually.