Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Firebase: How many snapshot listeners is my query causing?

Firebase has a limit of 100 snapshot listeners per client. I have a Screen called "MyChats" which displays all the chats a user has and wanted to know the following:

  1. Is my below function, which gets the data for the screen, counting as just a single listener?
  2. Would grabbing the latest message for each chatroom (to list it in a preview, similar to Whatsapp, FB Messenger and other chat applications) affect my number of listeners?
firestore
  .collection("chats")
  .where("membersArray", "array-contains", currentUser.uid)
  .where("deletedAt", "==", null)
  .orderBy("lastMessage.date")
  .startAt(new Date())
  .onSnapshot(...);

Here is a screenshot of the data structure

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Is my below function, which gets the data for the screen, counting as just a single listener?

You are calling onSnapshot() only once so that’s only 1 listener.

Would grabbing the latest message for each chatroom affect my number of listeners?

If you are referring to above code, that’s just a single query with 1 listener. If you individually add a listener for each document then that’ll be N listeners.

const col = db.collection("chats");

// 1 listener irrespective of number of documents in collection
col.onSnapshot(...)


// 3 listeners, as they are assigned separately
col.doc("1").onSnapshot(...)
col.doc("2").onSnapshot(...)
col.doc("3").onSnapshot(...)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading