0

i am makin a chating app and the user needs to be notified wheneer new notification comes. so i am using firebase admin sdk onSnapshot function.

but the problem is how can i listen to changes deep inside the multiple collection and documents.

i wanted to do this

const admin = require("firebase-admin");
const firebaseAdminKey = require("./firebase-admin-key.json");

admin.initializeApp({
  credential: admin.credential.cert(firebaseAdminKey),
  databaseURL: `https://${firebaseAdminKey.project_id}.firebaseio.com`,
});

let firestore = admin.firestore();
let messeging = admin.messaging();
let storage = admin.storage();
console.log("Firebase Admin Initialized");

firestore
  .collection("users")
  .doc("{userID}")
  .collection("chats")
  .doc("{senderID}")
  .collection("messages")
  .onSnapshot((snapshot) => {
    console.log("New Message ", snapshot.docs);
    snapshot.docChanges().forEach((change) => {
      console.log("New message: ", change.doc.data());
    });
  });

But i doesn't work in admin sdk

NOTE:- I AMM NOT DEPLOYING THIS API TO FIREBASE CLOUD SO I CAN NOT USE FIREBASE FUNCTIONS

this is what i was expecting but it works just between one user and sender.

const admin = require("firebase-admin");
const firebaseAdminKey = require("./firebase-admin-key.json");

admin.initializeApp({
  credential: admin.credential.cert(firebaseAdminKey),
  databaseURL: `https://${firebaseAdminKey.project_id}.firebaseio.com`,
});

let firestore = admin.firestore();
let messeging = admin.messaging();
let storage = admin.storage();
console.log("Firebase Admin Initialized");

firestore
  .collection("users")
  .doc("Zcpq0NHMODTGMT2Qry53ylksmvp2")
  .collection("chats")
  .doc("m79kGeXHKPNEgteCqxn2RG4eytL2")
  .collection("messages")
  .onSnapshot((snapshot) => {
    console.log("New Message ", snapshot.docs);
    snapshot.docChanges().forEach((change) => {
      console.log("New message: ", change.doc.data());
    });
  });

every user hase multiple friends i.e sender so how can i do it

theres one way ican think of i.e itrating over every user and sender but it will be very costly for my api as it could have time complexity of O(nk)

where k is average number of friends of each user.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Yash Sahu
  • 1
  • 2

1 Answers1

0

With the SDKs for Firestore there's only two ways to listen to a collection or query:

  1. To a single collection with firestore.collection('exact/path/to/the/collection').onSnapshot.
  2. To a collection group, which listens to all collections with a certain name, with firestore.collectionGroup('collectionName').onSnapshot.

In both cases you can then use a query to limit the results, and in the case of collection group queries you can tweak this to listen to a specific path, as shown in this answer to: CollectionGroupQuery but limit search to subcollections under a particular document

Beyond that there is no way to listen to a path structure similar to what Cloud Functions allows.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807