The snapshots-in-sync event indicates that all listeners affected by a given change have fired, even if a single server-generated change affects multiple listeners.
When a client calls disableNetwork() and then later enableNetwork() snapshotsInSync() seems to
fire before all listeners affected by a single change have been notified. This only happens
when testing against the Firestore emulator. When testing against a real backend snapshotsInSync()
behaves as expected.
Requires Node.js and the Firebase CLI
(npm install -g firebase-tools), which runs the emulator. The emulator also needs Java.
npm install
Two terminals:
# 1. the emulator
firebase emulators:start --only firestore --project demo-emulator-bug
# 2. the repro
node repro.mjs
It runs CYCLES (default 20) cycles, prints how many sync events fired while the two collections
disagreed, and exits.
The script creates two independent clients: a writer and a listener. The listener watches
the collection_a and collection_b collections and registers a snapshotsInSync() listener.
MODE=reconnect(default): the listener callsdisableNetwork().- The writer commits one
writeBatchcreatingcollection_a/<run>-<cycle>andcollection_b/<run>-<cycle>, and waits for the commit to complete. MODE=reconnect: the listener callsenableNetwork(), so the whole batch reaches it as one catch-up.MODE=steadyskips steps 1 and 3, so the batch arrives at a continuously connected client.- The listener waits until both collections have received the batch.
If snapshotsInSync() fires before both listeners have received the same update, the output is
marked <-- INCONSISTENT to indicate that snapshotsInSync() has fired too early.
The shape to look for (illustrative, not a recorded run):
[09:01:04.411] cycle 7: committed one batch to collection_a + collection_b
[09:01:04.423] snapshot collection_a: cycle 7 arrived
[09:01:04.423] snapshotsInSync() fired [collection_a=7, collection_b=6] <-- INCONSISTENT
[09:01:04.424] snapshot collection_b: cycle 7 arrived
[09:01:04.425] snapshotsInSync() fired [collection_a=7, collection_b=7]
=== mode=reconnect target=emulator cycles=20 ===
sync events while the collections disagreed: 20
firebase-js-sdk 13.x.x on node v23.11.0
When reporting results, also include the output of firebase --version.
| variable | default | meaning |
|---|---|---|
TARGET |
emulator |
emulator (at localhost:8080) or real |
MODE |
reconnect |
reconnect or steady (see above) |
CYCLES |
20 |
how many batches to commit |
The same program, pointed at a real backend, is the control group and the bug is not observed:
TARGET=real FIREBASE_PROJECT_ID=... FIREBASE_API_KEY=... node repro.mjs
The project's security rules have to permit collection_a and collection_b.