Thanatos
Thanatos

Reputation: 292

How much data and power does firebase child listener consume?

I have taken a look at earlier posts regarding firebase child listeners (How firebase listener actually work?). But I still could not find what I was looking for.

I have an android app with a foreground service that runs full time and that has a child listener in it. Based on any childAdded/updated/deleted operations, some operations are performed by the service.

The problem I face is that there is lot of power usage and it has to do with the internet data sent/received (based on battery reading). I am pretty sure that the child listener has been optimized to reduce power and network consumption.

I would like to know how the child listener works (i.e. exponential backoff, etc.) in the following scenarios:

1) There is NO child added on that child-listener reference node for over 20 minutes

How often does the android device connect to the child listener in this case? And will there be any power consumption due to maintaining the open socket connection with the server?

2) After 60 minutes of no changes on the firebase node, suddenly there is a child added/updated/deleted operation and the listener fires.

What is the power consumption in this case? And does the device again fire a 3.5kB overhead to reconnect with the server?

3) If I turn off database (for firestore or realtimeDB) using the goOffline method (or the equivalent method for firestore), how much time does it take before the connection is disconnected? Based on some android profiling that was done by me, I found a nearly five minutes gap before the connection was terminated!

Upvotes: 0

Views: 851

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

As long as there is an active listener, there is an open socket between your Android device and the Firebase Database server that it is connected to. There is no "exponential backoff" in that case, the listener just stays connected.

If you don't want to keep such an open connection, you can periodically calls addListenerForSingleValueEvent or explicitly call goOffline()/goOnline() from your code. A third option is to access Firebase through its REST API.

Each time the client reconnects to the server, it'll have to go through an SSL handshake. There is no way to prevent this on Android.

The 5 minute delay is likely just the socket time-out on your device. There is nothing the Firebase client can do about that, as it's a normal part of the operation of sockets.

Upvotes: 1

Related Questions