Baahubali Baahubali - 20 days ago 10
Android Question

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?

Answer

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).