Emre Esen
Emre Esen

Reputation: 137

Firebase cloud functions stop observer from another function

First function observe any process. However, if first function will not send any response, i want to stop observer manually according to second function.When i call second function, observer does not stop in first function. How can i handle this situation ?

exports.startObserverFunc = functions.https.onRequest((req,res) => {
            var userFirebaseID = req.query.firebaseID;
            var ref = admin.database().ref("Quickplay");
            let callback = ref.child(userFirebaseID).on("value",function(snapshot){
              if (snapshot.exists()){
                // Some Codes
                ref.child(userFirebaseID).off("value", callback); 
                res.status(200).send({
                  resultName: "ok"
                });

              }
            });

          });

exports.stopObserverFunc = functions.https.onRequest((req,res) => {

            var userFirebaseID = req.query.firebaseID;
            var ref = admin.database().ref("Quickplay");
            ref.child(userFirebaseID).off("value");  
            res.status(200).send({
              resultName: "ok"  
            });

          });

Upvotes: 0

Views: 89

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317322

You should avoid using observers/listeners like this in Cloud Functions. It will likely not do what you want, given that your code could be running on any number of server instances, based on the load on your functions. Also, two function invocations are definitely not going to be running on the same server instances, so they have no knowledge of each other.

It's almost certainly the case that you just want to use once() to query for a single object just one time, and use that in your response. This is what all the official samples do.

Upvotes: 1

Related Questions