Suppose you find yourself in a scenario where your application is growing immensely and you think it may be time to add a feature that brings more value to your application. You may have even considered adding an in-app chat feature, or what's commonly referred to as a real-time messaging feature, to help your users do even more with your application.
To build a chat feature quickly and ship it to users, you need an out-of-the-box solution. At the moment, one popular solution is Firebase, which is owned by Google and offers a backend as a service (BaaS) to facilitate building web and mobile applications. Its suite of services includes hosting, storage, authentication, push notifications, NoSQL databases, and analytics. With Firebase, you get to focus on building the frontend application without worrying about the backend. However, Firebase has some limitations, and there are other options available to consider that are more scalable and secure.
In this article, you'll learn about Firebase, its pros and cons, as well as how to build a chat app using Firebase. You'll learn that while Firebase is sufficient in some cases, there are options (like CometChat) that can provide you with a more scalable, secure, and fully featured experience for your users.
What Is Firebase?
Firebase is one of the easiest and most beginner-friendly BaaS offerings in the market, and it saves you time and resources when planning out and building your application.
When leveraging Firebase, you can use Firebase Hosting to host your web application; Firebase Cloud Firestore to store, sync, and query data; Firebase Cloud Storage to store and serve photos and videos; Firebase Authentication to authenticate users; and Firebase Cloud Messaging to send push notifications. All these services are offered on a single platform, making upgrades and management less stressful.
Benefits of Firebase
One of the major benefits of Firebase is that it offers a generous free tier. It currently offers two plans to developers: the Spark plan, which is the default for all new projects, and the Blaze plan, which is a pay-as-you-go option.
No matter which plan your project is on, Firebase offers you free features that are especially beneficial if you only have a few hundred daily users. For instance, you get 10,000 user authentications, 10 GB hosting storage, and 5 GB cloud storage for free each month. In addition, you get 20,000 writes per day and 50,000 reads per day on the Firestore database. Some services, such as analytics and cloud messaging, are entirely free to use with no limits.
Another benefit of Firebase is its wide range of services. Since it's a fully-fledged BaaS, you can build an entire enterprise application leveraging only Firebase services. For example, you can build authentication using Firebase Authentication, store application data in the Cloud Firestore database, store user-generated content (images and videos) in the cloud storage, use Firebase hosting to host the application and analytics to get insights on the app usage, and Firebase Crashlytics to detect and report bugs.
In addition, Firebase provides well-organized and detailed documentation that is beginner-friendly, with a lot of code samples to get you started. For each service offered, there is a guide on how to get started on all platforms it supports as well as an advanced section for the not-so-direct implementations of a service.
Disadvantages of Firebase
One of the main disadvantages of Firebase is that it is very Android-centric. Since Firebase is owned by Google, most of its services are optimized to be used on the Android platform. For instance, the Firebase Test Lab can integrate seamlessly with Android Studio while supporting a wide range of Android devices, which is not the case for iOS development, which only has basic features available on the Test Lab.
In addition, Firebase pricing steeply increases when scaling. For instance, the Firebase Realtime Database only stores 1 GB of data for free, and surpassing the limit costs you $5 USD per GB. Scaling is also limited by querying capabilities, as you can only have 200,000 simultaneous connections on a database.
In regard to database security, it's the developer's responsibility to configure who has read or write access and whether authentication is required. This is a security risk, as a misconfiguration will make the database vulnerable.
Moreover, there is limited data migration support. Since Firebase hosts your data, there is no option to transfer it to another backend service platform, which limits your options. This makes you dependent on the platform, and there's no way to cut ties and easily move your data to another platform.
Building a Chat App with Firebase
If you want to build a chat app leveraging Firebase, you have two databases you can consider: Firebase Realtime Database and Cloud Firestore. Of the two, Cloud Firestore is the better option since it's newer and builds on the successes of the Realtime Database with a more intuitive data model. It also automatically syncs the data between all clients whenever an update happens.
Cloud Firestore uses collections and documents to store data. A collection can have many documents, while a document can have many sub-collections. A collection contains only documents, and documents can contain fields that map to values as well as collections.
The data is stored in a hierarchical structure that grows linearly depending on the number of sub-collections and documents you have up to a maximum of 100 levels. This can easily get out of hand and become too complex if the database is poorly designed. This is why it's important to have a well-designed database, or your application will not work as expected with inconsistent data and unnecessary writes and reads happening.
For this chat app, you have a chats collection that holds multiple conversations as documents. The documents ID is generated from the two users' unique IDs (ie user1-user2). You must take great care to ensure that only a single document is created for every conversation.
A conversation has a name field, which is a description of the conversation, and a messages collection, which holds all the messages sent by the users. The messages collection holds documents with autogenerated IDs, and each has fields with information about the message sent and who sent it. However, adding fields, such as a typing indicator Boolean (which is constantly being updated), is challenging and requires careful planning and database organization.
To read and write a message, you need to follow the database structure as designed. This is done through APIs that are wrapped in the platform SDKs that Firebase offers. The following is a simple read to get a message in a conversation between two users:
let messageRef = db .collection("chats").document("user1-user2").collection("messages").document("id6qmVdg61peYsJzTfRn")
Securing the database is done by writing security rules. You can write rules to allow access to anyone, deny all access, or require authentication before accessing documents. This is a risky affair as developers can never be sure when they need to deny or allow access, leaving the database vulnerable.
If you're worried about pricing, security, and scalability, you may want to consider a straightforward and out-of-the-box solution. While the Firebase chat app can be sufficient for simple implementations, choosing a chat-dedicated solution means you don't have to think about how to design the database, optimize queries, or improve the speed of read-write operations.
If you're looking for a dedicated chat specialist, consider CometChat. CometChat offers more scalable, secure, fully featured experiences for your application users. With open source UI kits and no-code chat widgets, you can easily integrate chat into your application with features such as one-on-one chats, group chats, typing indicators, read receipts, and more. This means you don't have to worry about managing a database structure.
Firebase is an easy and beginner-friendly BaaS platform with multitudes of services that, when used together, can make your application great. It lets you quickly validate ideas and products at little to no cost to you. Building a chat app with Firebase services is simple, and with their auto-sync databases, you can easily ship chat features to your users.
However, if you're looking for an even better user experience, you may want to consider a dedicated chat solution. Dedicated solutions can ensure privacy for both parties, saving you time and resources while making your job easier by doing all the heavy lifting and mitigating security risks.
CometChat is a dedicated solution that enables start-ups to quickly launch their products to meet business needs. It brings you fully-fledged chat solutions with its no-code widgets or through its platform SDKs. With CometChat's comprehensive library extensions, you can optimize user engagement, security, and so much more.