I'm having delayed values from the esp32 (Firebase)

i have a esp32 card connected to firebase database realtime and i want to get 2 values from the firebase whenever there is a changed value, the problem is that i do get the values but it slows down the rest of my code, and if i remove the code where i’m getting values from the firebase, everything works just fine.
I think the fact that im always receiving data slows down the esp32 card.
so is there a solution where i can get data from Firebase whenever it changes ?

#include <Keypad.h>
#include <Wire.h> 
#include <WiFi.h>
#include <FirebaseESP32.h>


#define WIFI_SSID "dlink"             

                                           
#define FIREBASE_HOST "--------------------"                      
#define FIREBASE_AUTH "------------------"        

const byte ROWS = 4; //four rows

const byte COLS = 4; //three columns

char keys[ROWS][COLS] = {

  {'1','2','3','A'},

  {'4','5','6','B' },

  {'7','8','9','C'},

  {'*','0','#','D'}

};

byte rowPins[ROWS] = {14, 27, 26, 25}; //connect to the row pinouts of the keypad

byte colPins[COLS] = {33, 32, 13, 12}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

FirebaseData firebaseData;
int pos=0;
char password[5] ="1234";
char inputKey[16];

void setup(){
   Serial.begin(115200);
  WiFi.begin(WIFI_SSID);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
 Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);             //connect to Database
  delay(1000);

}

void loop(){
 
    Firebase.getInt(firebaseData,"state",state);
    Serial.println(state);
    Firebase.getString(firebaseData,"Motdepasse",pass);
    Serial.println(pass);
    pass.toCharArray(password, 5);

  
char whichKey = keypad.getKey();

if(whichKey)
    {
      if(whichKey == '*' || whichKey == '#'  || whichKey =='C' )
      {
     
      Serial.println("Invalid Key!");
      }
      else if(whichKey != 'D' && whichKey != 'A')
      {
    
       inputKey[pos]=whichKey;
       Serial.println(whichKey);
       pos++;
       
      }
      

    if(whichKey =='D' && pos >0)
     { 
      Serial.println("Deleted key");
      pos--;
      inputKey[pos]=NULL;  
    
           
     }

    if(whichKey =='A')
    {
       int i=0;
       boolean test = true;
       while( pos == 4 && i<pos && test == true)
        {
          if(inputKey[i] == password[i])
          {
           i++;
          }
          else
          test =false;
       }

        

    }
    
   }
 
 
}

If you continuously poll the database, it's no surprise that it slows things down.

How urgent is it that you know something has changed? Could you check once a minute? Less frequently?

What is changing the data in Firebase? Could it send a message to your ESP32 to tell you to check the database? Or indeed tell you what the new data is?

Does Firebase offer database triggers? If so, can it message your ESP32 when data gets written on either of the two fields you're watching?

Consider using MQTT.

I have an android app, and everytime i click on a button in the app it sends a value "state" to the firebase, and after that the esp32 receives this value and do some control with it, for example it changes the servo motor from locked to unlocked position.
So i need to constantly receive these values
i did find something similar to this problem on the internet and they used if(Firebase.available()) { Firebase.getInt(firebaseData,"state",state); }
but it didn't work for me because there is no Firebase.available() function in the FirebaseESP32.h library

kyrenna:
I have an android app, and everytime i click on a button in the app it sends a value "state" to the firebase, and after that the esp32 receives this value and do some control with it, for example it changes the servo motor from locked to unlocked position.

The Firebase is necessary?

We are providing similar functionality with MQTT broker and simply REST API, you can integrate it whatever you want.

HTML REST API example: WEB API examples | web-api-examples
Arduino library example: GitHub - IoTGuruLive/theiotguru-arduino-library: The IoT Guru Arduino integration library (03_mqtt_send_and_callback.ino) example.

The library's Github page provides all the information.

You need to use the stream.

Here is an example of the stream.

https://github.com/mobizt/Firebase-ESP32/blob/master/examples/Stream_callback/Stream_callback.ino

There are many examples in the library's examples folder.

vibrologic:
The library's Github page provides all the information.

You need to use the stream.

Here is an example slitherio of the stream.

https://github.com/mobizt/Firebase-ESP32/blob/master/examples/Stream_callback/Stream_callback.ino

There are many examples in the library's examples folder.

Thanks for your advice.