0
votes

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?

1
Publish-subscribe model - your app waits for updates, Firebase notifies the device over network connection when updates happen. - OneCricketeer
Also worth to mention: Firebebase Database (both frontend and backend) uses WebSockets which is what enables the client to subscribe to changes in the server over the network. - ArneHugo
Great comments from both of you. Who wants to turn it into an answer? - Frank van Puffelen
@cricket_007 Thanks a lot. Can you please name the Class/Object/Function in the code that is responsible for syncing with the server? I mean there should be some code statement responsible for this like ValueEventListener mentioned above? Does declaring an object of FirebaseDatabase includes syncing within? Would be great if you post an answer to this and with bit of more in-depth information if possible. - Baahubali
@ArneHugo see above. Thanks. :) - Baahubali

1 Answers

3
votes

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.