1

I am listening for new Firebase Realtime Database documents with code something like this:

firebase.database().ref(path)
  .orderByChild('timestamp')
  .on('child_added', snap => {
    ...
  });

where timestamp is set on the server with firebase.database.ServerValue.TIMESTAMP. I would like to have documents always handled in timestamp order, but I am aware that documents I add locally may arrive in the above code out of order.

I can check for and fix mis-ordered arrivals but I'd prefer not to if there is some way to have this not happen. I know about this answer (and answers that link to it) but I believe that applies to an earlier API without ordering methods like orderByChild.

I believe that I should be able to get timestamp order if I always add documents using a transaction and pass false in the applyLocally argument. I am wondering if it also works to add documents from a separate Javascript context on the same client (e.g. from a Web Worker) without a transaction.

Will either or both of these approaches guarantee timestamp ordering? Is there any other way to achieve this? Among approaches that work, is one clearly superior or are there trade-offs among them?

rhashimoto
  • 15,650
  • 2
  • 52
  • 80

1 Answers1

1

The local estimate/latency compensation event is only fired on the client that performs the write operation. So if you perform a write operation in a different context, the original context will only see the operation when it comes from the server.

You might even be able to accomplish this by using two FirebaseApp instances, although I couldn't get that working in a quick test here myself.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • I was hoping that was the case, but I wasn't sure if the library used some origin resource (like IndexedDB) that would complicate matters. Thanks! – rhashimoto Apr 30 '20 at 05:20