Nowadays, messenger apps are very widespread and everyone uses a plenty of popular Chat App such as Facebook Messenger, Whatsapp, Viber, Telegram and so on. However, the user has many choice to connect to the others, the reasons why many business built their own chat apps is because they concerned about security or privacy, and it is needed to manage conversation of their staff or require facilitation without switching many other application to make conversation.

What is Firebase

Firebase is Google’s mobile platform that helps you quickly develop high-quality apps and grow your business.

Firebase provide cloud platform which pay for what you use. It means instead of building your own server in order to store your data, optimize and maintain your hardware, make effort to scale up your system when the number of users have increased, you are able to use Firebase to resolve all of above issue with reasonable expense. In addition, on this subject matter of Chat App, Firebase offers 2 cloud base databases which are Realtime Database and Firestore:

Cloud Firestore_ is Firebase’s newest database for mobile app development. It builds on the successes of the Realtime Database with a new, more intuitive data model. Cloud Firestore also features richer, faster queries and scales further than the Realtime Database._

Realtime Database_ is Firebase’s original database. It’s an efficient, low-latency solution for mobile apps that require synced states across clients in realtime._

The reasons why you should choose Firestore is:

  • Client-first SDKs, with no servers to deploy and maintain
  • Realtime updates
  • Free tier, then pay for what you use

Furthermore, maybe you just want to try something new and see how it is.

Cloud Firestore is a cloud-hosted, NoSQL database that your iOS, Android, and web apps can access directly via native SDKs. Cloud Firestore is also available in native Node.js, Java, Python, Unity, C++ and Go SDKs, in addition to REST and RPC APIs.

Structure of the Database

About to be mentioned Firebase is NoSQL data model, you store data in documents that contain fields mapping to values. These documents put into collections which is used to organize your structure and build queries. The Cloud Firestore data model supports whatever data structure works best for your app. So far that your database is simply a large JSON object.

Definition of collection

Before the database and the collection is created, we need to create project and database in Firebase, else, there is basic guild to start with Chat App using Firebase.

Enable Google Authentication in Firebase

First of all, collection of user is mandatory, you can store data of user in Firebase or not, data of user can be stored in your own database using Oracle, PostgreSQL, MySQL, SQLServer and so on, and just using your userId or something like that to identify user in Firestore. If you store data of user in your own database on your server or the other cloud platform, you have to assurance the availability and the connectivity of your server. To simplify to everyone, I will create user’s collection to store user data when user sign in via Google Authentication.

To enable Google authentication in Firebase, we go to “Authentication” to left side navigation and choose “Sign-in method”, after clicking in pencil on right side of “Google” and enable it. Now we can sign in Firebase via Google Authentication.

Collection of User

When user signed in Firebase platform, each user data should be saved in collection and we just save some basic information of user and user unique id is used as key of document. The structure of this collection will express something like this:

Image for post

User Collection

Creating **User collection **with basic detail such as uid, email, photoUrl, displayName (depend on your requirements).

In this case, gathering information of signed in user is not problem. The below sample can help you sign in via Google Authenticate and get information of user after sign in using Javascript.

const auth = firebase.auth()
signInWithGoogleAuthentication() {
  const provider = new firebase.auth.GoogleAuthProvider()
  return new Promise((resolve, reject) => {
      .then(function (result) {
      .catch(function (error) {

This is sample of saving information of user to Firestore:

const db = firebase.firestore()
saveUserToFirestore(user) {
  const userRef = db.collection('user')
    uid: user.uid,
    displayName: user.displayName,
    photoURL: user.photoURL,

Next one, we need to take care about group chat because one user can join multiple group and one group will contain multiple users. So that we should create “Groups” in user’s collection to optimize queries and avoid to take longer than expected to get results back from a complex query.

#database #firebase #firestore

Structure Firestore (Firebase) for Scalable Chat App
36.20 GEEK