2

I use the firebase-js-sdk (in a Vue PWA) to listen for realtime updates from Firestore.

To restore the app after it was in background i use „vuex-persist“ for the app state. Beside the app state i also need to reestablish the realtime query by calling .onSnapshot on the same collection with the same parameters.

I wonder if the firebase-js-sdk is able to reuse the realtime query, which was requested before the app went to background. Otherwise i will be charged every time the user switches to background and back.

Thanks for any help, Thomas

Thomas
  • 31
  • 5
  • Firebase queries need to be "unlistened" and that's the only way for it to stop listening unless the whole process (app) is killed. So if you haven't removed the listeners and the app still exists, they should be listening. You can always confirm this by changing something in the database after your app is in the foreground again to see if you observe that change. – TheBen Aug 26 '18 at 22:03
  • To be a bit more specific. If the app runs on ios in "apple-mobile-web-app-capable mode" (in order to behave like a native app with homescreen icon and fullscreen), then a switch to background and back to foreground is like pressing F5 in a desktop browser. It reloads the app and everything needs to be restored, also the realtime listeners. This means: Observing db changes (like you mentioned) is not possible because the app has no firestore reference anymore. – Thomas Aug 27 '18 at 07:19

2 Answers2

1

Solution: The question was not about how to make the restore (including restore of realtime listeners) technically working. I was mainly interested in how to avoid to get charged everytime such a restore of a realtime listener happens.

According to those 2 answers:

i won't be charged if the following prerequisites are met:

  • I recreate the same query (which in turn generates the same resumeToken. A resumeToken is kind of a key which is used by firestore serverside to recognise queries again)
  • No data of the resultset of that Query has changed (firestore tracks queries for maximum 30 minutes and would invalidate the resultset if changes happend)
  • Persistance is enabled which enables the client sdk to serve locally cached resultsets
  • The resultset was generated within the last 30 minutes (maximum time a query is tracked by firestore). If it is older, then the query get's executed again and then i get charged for that.
Thomas
  • 31
  • 5
0

You should initialize your Firebase in your main.js or basically the page that's always initiated when app starts or brought to the foreground so that even if your listener is detached when you come to the foreground again, Firebase is initiated and so a new listener can be created.

So in your main.js you'll have something like:

import Firebase from 'firebase'
let config = {
    apiKey: "...",
    authDomain: "...",
    databaseURL: "...",
    storageBucket: "...",
    messagingSenderId: "..."
  };

let app = Firebase.initializeApp(config)
let db = app.database()
let booksRef = db.ref('books') 
//....etc///

If you have the setup correctly and set up your listeners in a method that's run each time the page is loaded, you always have a listener. Just make sure you avoid creating multiple listeners.

TheBen
  • 3,410
  • 3
  • 26
  • 51