LukeWarm2897
LukeWarm2897

Reputation: 179

Firebase: First write is slow

Currently developing a hybrid mobile app using ionic. When the app starts up, and a user writes to the Realtime Database for the first time, it's always delayed by around 10 or more seconds. But any subsequent writes are almost instantaneous (less than 1 second).

My calculation of delay is based on watching the database in the Firebase console.

Is this a known issue, or maybe I am doing something wrong. Please share your views.

EDIT:

The write is happening via Firebase Cloud Function.

This is the call to the Firebase Cloud function

this.http.post(url+"/favouritesAndNotes", obj, this.httpOptions)
  .subscribe((data) => {
    console.log(data);
  },(error)=>{
    console.log(error);
  });

This is the actual function

 app.post('/favouritesAndNotes', (request, response) => {
  var db = admin.database().ref("users/" + request.body.uid);
  var favourites = request.body.favourites;
  var notes = request.body.notes;
  if(favourites!==undefined){
    db.child("favourites/").set(favourites);
  }
  if(notes!==undefined){
    db.child("notes/").set(notes);
  }
  console.log("Write successfull");
  response.status(200).end();
});

Upvotes: 1

Views: 1334

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

The first time you interact with the Firebase Database in a client instance, the client/SDK has to do quite some things:

  1. If you're using authentication, it needs to check if the token that it has is still valid, and if not refresh it.
  2. It needs to find the server that the database is currently hosted on.
  3. It needs to establish a web socket connection.

Each of these may take multiple round trips, so even if you're a few hundred ms from the servers, it adds up.

Subsequent operations from the same client don't have to perform these steps, so are going to be much faster.

If you want to see what's actually happening, I recommend checking the Network tab of your browser. For the realtime database specifically, I recommend checking the WS/Web Socket panel of the Network tab, where you can see the actual data frames.

Upvotes: 6

Related Questions