lei lei
lei lei

Reputation: 1829

Why Firebase realtime database react even through the network is disconnect?

In my application, I send a message to firebase realtime database by using function sendMsg(), and monitor the new arrived messages in DB by using function monitorMsg(). To my understanding of firebase database, if the device disconnects with the network(both mobile data and WiFi), when I send Msg, I should get the err and monitorMsg() function should not work of course. But the result is opposite, sendMsg() doesn't catch an err and monitorMsg() takes action. I could not understand why this happen, do not the firebase get and set function depend on network connection?

monitorMsg(){this.firechats.child(userId).child(toUserId).orderByChild("time").startAt(time).on('value', (snapshot) => {
              console.log("why this happen?????????")
              console.log(snapshot)
            });}



sendMsg(){
var promise = new Promise((resolve, reject) => {
      this.firechats.child(userId).child(toUserId).push().set(msg).then(() => {
        console.log("send msg to firebase")
      }).catch((err)=>{
        console.log("send to toUserId failed")
        reject(err)
      })
    })
}

Upvotes: 0

Views: 407

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317740

Everything you are observing is normal and expected.

With Realtime Database, writes don't fail due to lack of connectivity. They simply get queued up and synchronized later when connectivity returns, so you don't have to do any work to survive a temporary loss of connection. Writes only fail if the server rejects the write, for example, by violating a security rule or some limitation.

Also, local listeners will fire immediately with local changes, even without a connection. This is also part of what makes it so easy to write apps that work well while offline. If, for some reason, the write is eventually rejected by the server, the listener will get invoked with another change that will "undo" the first one, so the listener will always see the most accurate view of the data.

Upvotes: 1

Related Questions