1

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}`);
            });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Fabrizio
  • 514
  • 7
  • 18

1 Answers1

1

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}`);
   });
Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • First thank you so much for your quick and detailed reply. – Fabrizio Apr 30 '20 at 15:00
  • I changed the code and I understand that it's much correct in the way you wrote and it seems to me that I got faster reply. I also checked and I'm using host us-central1, I made other test after not using the agent for some hours and the first two queries didn't return any values, starting from the third it worked ok. – Fabrizio Apr 30 '20 at 15:14
  • What I see on the GCP log is an error that I can't understand: "dialogflowFirebaseFulfillment Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail". Also I read a warning: "Accessing deprecated metadata server endpoint at "/computeMetadata/v1beta1/instance/service-accounts/default/token" but I don't where to investigate. – Fabrizio Apr 30 '20 at 15:19
  • 1
    If you are running this using Google Cloud Functions, you may wish to add some additional logging to see where in your code slowdowns are taking place and update your question with what the logs are showing. To help further, it would also be useful if you updated your question with as much information about how you are deploying your code, further details about your code and runtime environment, etc. – Prisoner Apr 30 '20 at 22:33