Fabrizio
Fabrizio

Reputation: 504

Firebase Realtime Database first query not responding

Hi there and thanks for reading this.

I'm learning how to work with Dialogflow and Firebase Realtime Database and I like these platforms a lot. I created a very simple DB structure on Firebase with 7 fields and in my agent I query them with a very simple fulfillment. It seems to be working but every "first query" that I do the next day seems to last about 5000ms so the DB doesn't respond: starting from the second query it works almost in real time so it seems to be sleeping or something. In my today test at the first query I read this in the Dialogflow log: "webhook_latency_ms": 4663 but at least it worked, generally it doesn't.

It seems like there's some uncertainty about getting data from the DB.

Any suggestion would be very appreciated.

The realtime database structure is this:

serviceAccount bitstream: "pluto" cloud: "paperino" data center: "gastone" datacenter: "gastone" ull: "bandabassotti" vula: "minnie" wlr: "pippo"

and this is how I query Firebase:

 const servizi = agent.parameters.elencoServiziEntity;      
        return admin.database().ref("serviceAccount").once("value").then((snapshot) =>
            {  
                var accountName = snapshot.child(`${servizi}`).val();
                agent.add(`L'Account Manager del Servizio ${servizi} si chiama:  ${accountName}`);
                console.log(`${servizi}`);
            });

Upvotes: 0

Views: 528

Answers (1)

Prisoner
Prisoner

Reputation: 50701

The webhook latency isn't always related to the database call - it includes the time that may be required to start the webhook itself. If you're using Firebase Cloud Functions or the Dialogflow Built-In Code Editor (which uses Google Cloud Functions), there is a "cold start" time required to start the function. If your webhook is running somewhere else, on AWS Lambda for example, you may have network latency in addition to the cold start time.

There is very little you can do about this. If you're running with one of Google's Cloud Function solutions, make sure you're running them in the Central-1 region, which is close to where Dialogflow also runs. To avoid the cold start completely - run a server.

Usually, however, the latency and cold start time shouldn't be that long. Which suggests that your code is also taking a while to run. You may wish to look at your logs to see why execution time is taking so long - the call to the Firebase RTDB may be part of it, but there may be other things causing a slowdown that you don't show in your code.

One thing you are doing in your call to Firebase is pulling in the entire record, instead of just pulling in the one field that the user is asking for. This does require more data to be marshaled, which takes more time. (Is it taking a lot more time? Probably not. But milliseconds count.)

If you just need the one field from the record the user has asked for, you can get a reference to the child itself and then do the query on this reference. It might look like this:

   const servizi = agent.parameters.elencoServiziEntity;      
   return admin.database()
     .ref("serviceAccount")
     .child(servizi)
     .once("value")
     .then((snapshot) => {  
       const accountName = snapshot.val();
       agent.add(`L'Account Manager del Servizio ${servizi} si chiama:  ${accountName}`);
       console.log(`${servizi}`);
   });

Upvotes: 1

Related Questions