Java – How to build a chat with firebase

How to build a chat with firebase… here is a solution to the problem.

How to build a chat with firebase

I’m using Cloud Firestore (NoSQL) to store profile information for the following users:

  "uid": "abc123",
  "name": "...",
  "friends": [
    "uid": "x234", 

Now I want to know how to build a direct chat between users. I’m thinking about it:

Add an additional field for each user document, for example:

"chats": [
    "from": "name",
    "message": "...",

Or instead of using Firestore for chat, I consider using a Firebase real-time database with a similar structure.

The benefit of this last approach is that user documentation doesn’t become “bloated” with a large number of chat protocols.

I need some structure/implementation suggestions that best fit this use case.


When you start building an application, you first need to consider the database that works best for it. If you’re considering Firestore, you need to know that Cloud Firestore’s pricing model is for applications that perform a large number of small reads and writes per second per client, which can be much more expensive than applications with similar performance in a live database.

There are also some differences between the two databases. If you want to continue using Firebase Realtime Database, you need to know that you can’t query multiple attributes, and it usually involves duplicate data or client-side filtering, which in some cases is some sort of confusion. The real-time database does not automatically scale, while Firestore does.

Regarding how to build a database for a chat application, you need to know that there is no perfect structure to do this. You need to structure your database in a way that allows you to read/write data very easily and very efficiently. Firebunker official documentation explains how to build a database for a chat application. If you want something more complex, read this article Structuring your Firebase Data correctly for a Complex App .

For a better understanding, I recommend that you also take the Firebase free class, Firebase in a Weekend: Android .

So it’s up to you to decide which one is right for you.

P.S: If you’re interested, I’m also > in my explain how to create chat apps using Cloud Firestore and Kotlin.