Baahubali
Baahubali

Reputation: 163

How does Firebase Database Instances retrieve data in realtime?

I just built my first app. It's an Group Messaging App for which I used Firebase Realtime Database. I followed this tutorial to build my app.

The chat is working flawlessly and in realtime ie any changes into the Database are retrieved and reflected within seconds on my app. Actually, being bit curious, I didn't just copy paste all those code lines instead I'm trying to understand meaning behind each statement. So, I'm confused with one of my doubts:

How does this work in realtime (chats pop up immediately)? I was reading about Firebase Database here and they mention ValueEventListener is used to update app data in realtime but what's used here?

Upvotes: 2

Views: 4113

Answers (1)

ArneHugo
ArneHugo

Reputation: 6509

From the documentation:

Realtime: Instead of typical HTTP requests, the Firebase Realtime Database uses data synchronization—every time data changes, any connected device receives that update within milliseconds.

Network-wise this is achieved through WebSockets, which is used both on the server and in the client side Firebase library.

Additionally, the "Realtime Database API is designed to only allow operations that can be executed quickly".

Edit: The Firebase client library sets up one WebSocket to communicate with the Realtime Database, which is used for all the communication with the Realtime Database, both reading/subscribing and updating/pushing (unless you use the REST API).

Edit 2: In the tutorial you did you used the FirebaseListAdapter which abstracts away how the data synchronization is done. It's fourth parameter is a reference to a Realtime Firebase Database location that it will sync with (using WebSocktes), and populate the list for you. It takes each entry of the synced data and puts it into new Java objects of the model class you provide as second argument, namely ChatMessage.class.

Upvotes: 8

Related Questions