Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How firebase listener actually work?

I'm wondering on Android, how the underlying actual mechanism work when you add an listener to the database. Is it just more frequent pulling or something else special?

Update: To make it clearer, I understand what a listener is, but I meant how does the 'listening' scheme work, how a client (Android) knows the data on the server changed. Is it just a periodical pulling? (and Firebase engineers already do the hard work to cover that and make it easy for us).

Looks like firebase is not open-source.

// Attach an listener to read the data at our posts reference
ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        System.out.println(snapshot.getValue());
    }
    @Override
    public void onCancelled(FirebaseError firebaseError) {
        System.out.println("The read failed: " + firebaseError.getMessage());
    }
});
like image 493
EyeQ Tech Avatar asked Jul 09 '16 07:07

EyeQ Tech


People also ask

How does Firebase listener work?

Firebase data is written to a FirebaseDatabase reference and retrieved by attaching an asynchronous listener to the reference. The listener is triggered once for the initial state of the data and again anytime the data changes.

What does Firebase actually do?

Google Firebase is a Google-backed application development software that enables developers to develop iOS, Android and Web apps. Firebase provides tools for tracking analytics, reporting and fixing app crashes, creating marketing and product experiment.

What does value event listener do?

Classes implementing this interface can be used to receive events about data changes at a location. Attach the listener to a location user addValueEventListener(ValueEventListener) .

How can I read data from Firebase without listener?

You can use Firebase's REST API to get access to the "raw" data. You can use any Firebase URL as a REST endpoint. All you need to do is append ". json" to the end of the URL and send a request from your favorite HTTPS client.


1 Answers

disclaimer: this is a simplified description of how things work at the time of writing. Things may have changed by the time your read it.

When your app connects to the Firebase Database, it opens a web socket connection from the device to a Firebase server. This connection stays open for the lifetime of your app or until you call goOffline().

When you attach a listener, the client sends the location (and potential query parameters) to the server. The server adds that listener to a list of all listeners of all connected clients. It then also sends back the initial data for that listeners.

Whenever a write operation is committed to the database, the server scans the listeners. For each relevant listener, the server sends an update to the client over the open web socket.

like image 195
Frank van Puffelen Avatar answered Sep 17 '22 18:09

Frank van Puffelen