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.