EyeQ Tech
EyeQ Tech

Reputation: 7358

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());
    }
});

Upvotes: 16

Views: 12977

Answers (3)

Frank van Puffelen
Frank van Puffelen

Reputation: 600126

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.

Upvotes: 31

Veeresh Charantimath
Veeresh Charantimath

Reputation: 4719

It happens Asynchronously

Adding listeners to a node reference will fetch any changes made to the node reference asynchronously

void onDataChange(DataSnapshot snapshot)

This method will be called with a snapshot of the data at this location. It will also be called each time that data changes.

void onCancelled(FirebaseError error)

This method will be triggered in the event that this listener either failed at the server, or is removed as a result of the security and Firebase rules. For more information on securing your data, see: Security Quickstart

Example

ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
    System.out.println(snapshot.getValue());
    Users users = snapshot.getValue(Users.class) //This is your POJO class
    String name = users.getName(); //Other getter methods to fetch data from firbase


}
@Override
public void onCancelled(FirebaseError firebaseError) {
    System.out.println("The read failed: " + firebaseError.getMessage());
}

});

Upvotes: 0

Wilik
Wilik

Reputation: 7720

It's explained in the guide Firebase - Retrieve Data on Android

This method is triggered once when the listener is attached and again every time the data, including children, changes.

Upvotes: -1

Related Questions