How to Build a React Native Chat App

App and web development have come a long way over the last few years. We use a lot of social applications, and chat applications every day. This tutorial will teach you how to create your own chat application with React Native, Firebase, and the CometChat UI Kit.

Hiep Le • Aug 11, 2022

App and web development have come a long way over the last few years. We use a lot of social applications, and chat applications every day. This tutorial will teach you how to create your own chat application with React Native, Firebase, and the CometChat UI Kit.

We will cover the following topics:

  • Registering users, login, and login out, we will use the Firebase Authentication service to create new accounts and authenticate users.

  • Using the CometChat React Native UI Kit. We will use the CometChatUI component to implement chat, voice, and video calling features, and the CometChatUserList component to show the list of users with the search bar. For more information about the CometChat React Native UI Kit components, you can refer to this link.

Prerequisites

To follow this tutorial, you must understanding the basics of using React Native.  You will also need to have Node.js installed.

Installing the App Dependencies

Create a new project named clubhouse with version 0.63.3 by running the following command

npx react-native init react-native-chat-app --version 0.63.3

Copy the dependencies from here and run the following command

npm i --force

You need to increase the minimum target version for ios in the Podfile to 11. For more information about how to setup CometChat in Android and IOS, you can refer to this link

Navigate to the ios folder and run pod install. Once all the pods are installed navigate back to the root folder.

cd ios && pod install && cd ..

In this application, we need to load SVG files. For this reason, we use the react-native-svg and react-native-svg-transformer libraries. Therefore, we need to update the metro.config.js file as follow.

https://gist.github.com/hieptlccc/45c6f2a1aab0b3fc86176b2983b6bf4b

Configuring the CometChat SDK

In this tutorial, we will be using the CometChat React Native UI Kit. When users log in to our application, they also log in to the CometChat platform.

We can configure the CometChat SDK following the steps below:

Head to CometChat and create an account.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61700dac76419b7bac2de69d_s_1988D50EEAEE632A7CEA2F32B0824BC13ED61EA41386BA94E99320DDC4403C74_1633934795404_image.png

Figure 1. Register a new CometChat account if you do not have one.

Log in to the CometChat dashboard

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/618ea49640681708de79e9c8_s_C05E7B93FFC9B007F6C09747840C5EEC81AFEC86C6C89C570966797151AE7076_1635316482378_Screen%2BShot%2B2021-10-27%2Bat%2B13.34.18.png

Figure 2. Log in to the CometChat Dashboard with your created account.

Add a new app, named chat-app

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61700dac7e33a881ced79837_s_1988D50EEAEE632A7CEA2F32B0824BC13ED61EA41386BA94E99320DDC4403C74_1633936628018_image.png

Figure 3. Create a new CometChat app - Step 1.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/618ea496d603ba7ef8cabeb3_s_C05E7B93FFC9B007F6C09747840C5EEC81AFEC86C6C89C570966797151AE7076_1635316586805_VLioyzP2.png

Figure 4. Create a new CometChat app - Step 2.

Select your new app from the list

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/618ea4969bad618548da9ccd_s_C05E7B93FFC9B007F6C09747840C5EEC81AFEC86C6C89C570966797151AE7076_1635316654035_bSCncNJN.png

Figure 5. Select your created app.

From the Quick Start, copy the APP_ID, REGION, and AUTH_KEY. You will need these later

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/618ea4968574a3c1945af0bd_s_C05E7B93FFC9B007F6C09747840C5EEC81AFEC86C6C89C570966797151AE7076_1635316757752_QHRb0reB.png

Figure 6. Copy the the APP_ID, REGION, and AUTH_KEY

Navigate to the Users tab and delete the default users

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61700dad71a9da9af1ce413b_s_1988D50EEAEE632A7CEA2F32B0824BC13ED61EA41386BA94E99320DDC4403C74_1634011719780_Screen%2BShot%2B2021-10-12%2Bat%2B09.16.43.png

Figure 7. Navigate to Users tab and delete all the default users

Navigate to the Groups tab and delete the default groups

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61700dad725840d1c9f570bc_s_1988D50EEAEE632A7CEA2F32B0824BC13ED61EA41386BA94E99320DDC4403C74_1634011883889_Screen%2BShot%2B2021-10-12%2Bat%2B09.17.03.png

Figure 8. Navigate to Group tab and delete all the default groups.

Create an env.js file in the root folder of your project. Edit your env.js file to export the CometChat App ID, Region and Auth Key for your app

export const cometChatConfig = {
 cometChatAppId: xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx,
 cometChatRegion: xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx,
 cometChatAuthKey: xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx
};

Note: include env.js in your .gitignore file to prevent it being tracked by git

Setting up a Firebase Project

We will be using Firebase to allow users to create accounts and store messages through their Realtime Database and Authentication services.

Head over to Firebase and create a new project.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/614c3c25b1343e399f552f39_s_97C12218C2C42467580F3CD3CC0FEA9DE0DCE7708763AA2477EAE3E80A560C0B_1630159114235_image.png

Figure 9. Firebase

Firebase provides support for authentication using different providers, For example, Social Auth, phone numbers, and email address and password combination.

Users will authenticate using an email address and password. By default this method is disabled, so we need to enable it.

Under the authentication tab for your project, click Sign-in method, you should see a list of providers currently supported by Firebase.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61700dadfe6797a98f91cec2_s_1988D50EEAEE632A7CEA2F32B0824BC13ED61EA41386BA94E99320DDC4403C74_1633156978745_image.png

Figure 10. Firebase Authentication.

Click the edit icon on the email/password provider and enable it.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61700dadafbf8d49913c5467_s_1988D50EEAEE632A7CEA2F32B0824BC13ED61EA41386BA94E99320DDC4403C74_1633157046969_image.png

Figure 11. Enable Firebase Authentication with Email and Password

Next, you need to enable the Firebase Realtime Database. This will be used to store user information in the app.

To set up the Firebase Realtime Database service in our application, we need to follow the below steps:

Choose the “Realtime Database” option from the navigation on the left

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/614c3c262de4bc62b9b6cd3f_s_97C12218C2C42467580F3CD3CC0FEA9DE0DCE7708763AA2477EAE3E80A560C0B_1630657058438_image.png

Figure 12. Choose “Realtime Database” option.

Click on the “Create Database” option to create a new Firebase database

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/614c3c266dcbb373f349c630_s_97C12218C2C42467580F3CD3CC0FEA9DE0DCE7708763AA2477EAE3E80A560C0B_1630657170655_image.png

Figure 13. Click on “Create Database".

Select the location where your realtime database will be stored

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/614c3c269a9ca3cacc1683c4_s_97C12218C2C42467580F3CD3CC0FEA9DE0DCE7708763AA2477EAE3E80A560C0B_1630658067148_image.png

Figure 14. Select location where your realtime database will be stored.

Select the “Start in test mode” option when setting up the database

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/614c3c2651778cb6487fc499_s_97C12218C2C42467580F3CD3CC0FEA9DE0DCE7708763AA2477EAE3E80A560C0B_1630658182709_image.png

Figure 15. Select “Start in test mode” for the learning purpose.

You should now be able to se your database in Firebase

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/614c3c2643e8af3df586fc93_s_97C12218C2C42467580F3CD3CC0FEA9DE0DCE7708763AA2477EAE3E80A560C0B_1630657383693_image.png

Figure 16. Database Url.

You need to set the .indexOn for some fields that we want to query in the app.  Select the “Rules” tab an edit it to index the email and fullname fields as follows

We will add the user's information to the Firebase Realtime Database after the user has registered successfully in our application.

We need to add a new field the .indexOn array if we want to fetch the user’s information using that field. In our application, we want to fetch the user’s information using their email or their full name. For this reason, the .indexOn array contains two elements - email and full name.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/62501e37a180510abe9d2fc6_s_9411495EAABDFE4D0EEDA9F5BE8B03F5B33FA3C93BD0FA51CB6A8535916C60A7_1648010413876_Screen%2BShot%2B2022-03-23%2Bat%2B12.38.56%2BAM.png

Figure 17. Update Firebase Realtime Database Rules.

We also need to upload images, and user’s avatars as part of the app, we will store these using the Firebase Storage service.

To enable the Firebase Storage service, please follow the steps below:

Click on the “Storage” option on the left, then click on the “Get started” button

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61c069b673c600620130674e_s_CD711F28ED418AC7E900AAB9DE0CB9B04B4580DCE3A553D5C1792000AD9D0F44_1637207969512_Screen%2BShot%2B2021-11-18%2Bat%2B10.56.34.png

Figure 18. Enable Firebase Storage Service.

Click on the “Next” button

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61c069b74456d174adccd9a9_s_CD711F28ED418AC7E900AAB9DE0CB9B04B4580DCE3A553D5C1792000AD9D0F44_1637208280735_Screen%2BShot%2B2021-11-18%2Bat%2B11.00.22.png

Figure 19. Click on the “Next” button

Click on the “Done” button

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61c069b754e7e88eefe03044_s_CD711F28ED418AC7E900AAB9DE0CB9B04B4580DCE3A553D5C1792000AD9D0F44_1637208311638_Screen%2BShot%2B2021-11-18%2Bat%2B11.01.05.png

Figure 20. Click on the “Done” button

Update the Firebase Storage rules so that everyone, who can access the application, can upload files

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61c069b65f03d97c29a60274_s_CD711F28ED418AC7E900AAB9DE0CB9B04B4580DCE3A553D5C1792000AD9D0F44_1637208338834_Screen%2BShot%2B2021-11-18%2Bat%2B11.02.35.png

Figure 21. Update the rules.

After registering the Firebase services (Firebase Realtime Database, Firebase Authentication, Firebase Storage), you need to go and register your application under your Firebase project. On the project’s overview page, select the add app option and pick web as the platform, and follow the guidelines. After doing that, you should see the below screen.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61c596d352f3c306c95b7575_s_CD711F28ED418AC7E900AAB9DE0CB9B04B4580DCE3A553D5C1792000AD9D0F44_1637207709035_Screen%2BShot%2B2021-11-18%2Bat%2B10.54.29.png

Figure 22. Firebase Dashboard

After you have setup the Authentication, Realtime Database and Storage services, you’ll be presented with a screen containing your application credentials which need to be added to your env.js file, like the example below

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/614c3c2544523c957a89846d_s_97C12218C2C42467580F3CD3CC0FEA9DE0DCE7708763AA2477EAE3E80A560C0B_1630656775626_image.png

Figure 23. Firebase credentials.

export const fbConfig = {
 apiKey: xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx,
 authDomain: xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx,
 databaseURL: xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx,
 projectId: xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx,
 storageBucket: xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx,
 messagingSenderId: xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx,
 appId: xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx,
 measurementId: xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx,
};

export const cometChatConfig = {
 cometChatAppId: xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx,
 cometChatRegion: xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx,
 cometChatAuthKey: xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx,
};

The image below shows the data structure that we will store in the Firebase Realtime database. In this tutorial, we just need to store the user information, and that information contains the avatar, email, list of followers, full name, and id.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/62501e398d11b276d533fc7a_s_9411495EAABDFE4D0EEDA9F5BE8B03F5B33FA3C93BD0FA51CB6A8535916C60A7_1645348385495_Screen%2BShot%2B2022-02-20%2Bat%2B4.12.24%2BAM.png

Figure 24. Data Structure - Users

Project Structure - Client Side

The image below reveals the project structure after we follow this tutorial. This section will talk about some important folders

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61c596d76c93627bf981ac04_s_CD711F28ED418AC7E900AAB9DE0CB9B04B4580DCE3A553D5C1792000AD9D0F44_1637207283843_Screen%2BShot%2B2021-11-18%2Bat%2B10.47.34.png

Figure 25. The Project Structure

  • components: this folder stores components that will be used in the application.

  • images: this folder contains images that will be used in the application such as audio icon, video icon, settings icon and so on.

  • env.js: this file stores configuration of the application such as Firebase and CometChat configuration.

  • context.js this file helps us store the state that will be shared across all components without passing down at every level. For this reason, we can avoid props drilling.

React Context

In this application, we will be using the React Context API to pass data between different components. the React Context API helps us pass data through the component tree without passing it down every level individually. Please create a context.js file inside the src folder. The full source code of the context.js file can be found here

Images

You can get a copy of all the images used in this application here. Create an images folder inside the src folder, and add all of the images from the link above

The Firebase.js File.

The firebase.js file will take responsibility to interact with the Firebase services. For this reason, we need to set up this file in our code base. To set up the firebase.js file, you need to create a firebase.js file inside the root folder of your project. Please refer to the below code snippet below, or the full source code can be found here.

Setting Up Common Services

We create some common services so that we can reuse them in different places. The services folder will contain all of the service files . A copy of the folder can be found here.

  • firebase.js will contain functions that will be used to interact with Firebase’s services, such as inserting new data toFirebase Realtime Database, or uploading files to the Firebase Storage. Please create a firebase.js file inside the services folder. The full source code can be found here.

  • ui.js will contain functions that are related to the UI, such as showing an alert message. Please create the ui.js file inside the services folder. The full source code can be found here.

The App.js File

The App.js file is responsible for rendering different components based on the state of the application. For example, it will render the login page if the user has not logged in, yet or it renders the home page if the user has signed in to the system.

It will also be used to initialize CometChat. The full source code of the app.js file can be found here.

The code below initialize CometChat using your CometChat API Credentials from the .env.js file.

The Login Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/62f51edaf7d03401db9fd24d_s_1FBE083E6A404BD576DEE9FADE73049F282CEBE22960A8ED45650A315C2C54FF_1652174361250_Screen%2BShot%2B2022-05-10%2Bat%2B4.19.03%2BPM.png

Figure 26. The Login Component

This component is responsible for authenticating our users. It accepts the users credentials and either signs them up or in, depending on if they are a new user or not. The steps below explain the authentication flow.

  • Step 1: Users input their credentials (email/password).

  • Step 2: The application validates the credentials.

  • Step 3: If the credentials are valid, the application calls the Firebase Authentication service using those credentials.

  • Step 4: If the Firebase Authentication service returns the user’s aid the application continues to log in to the CometChat using the returned uid.

  • Step 5: If the CometChat service returns a valid response, the application calls the Firebase Realtime Database service to get the authenticated information, and then store that information in Async Storage. When we need to get the authenticated information, we just need to get the data from Async Storage instead of calling the Firebase Realtime Database service.

The code below is responsible for how our app interacts with the Firebase Authentication service, and the CometChat SDK.

Please create a components folder inside the root directory of the project. The components folder should also contain a login folder with a file named Login.js. The full source code for the Login.js file can be found here.


We store the authenticated Information in the async storage so that users don’t have to login each time they visit the app.

To login to the CometChat, we call the loginCometChat function, you can refer to the below code snippet for more information. This function is used inside the Login.js file as we can see from the above code snippet.

The Sign Up Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/62f51eda6d5bbffcb2e3a7dc_s_1FBE083E6A404BD576DEE9FADE73049F282CEBE22960A8ED45650A315C2C54FF_1652174544279_Screen%2BShot%2B2022-05-10%2Bat%2B4.22.10%2BPM.png

Figure 27. The Sign Up Component

The sign-up component will help users to register new accounts. This component will do two things. The first thing is to register new accounts by calling the create user API. Aside from that, it also registers new accounts on CometChat by using the CometChat SDK.

Create a register folder inside the components folder, and create a SignUp.js file inside it. The full source code can be found here.

To create a new CometChat account, we call the createCometChatAccount function. This function will be used inside the SignUp.js as we can see in the above code snippet. Following that, you can refer to the below code snippet below for more information.

Add the CometChat UI to our Application

Before we can use the CometChat Pro React Native UI kit, we need to add it in our project so that we can reference it. In this case, we are using React Native UI Kit v3.0.6-3

https://github.com/cometchat-pro/cometchat-pro-react-native-ui-kit.git -b v3

  • Copy the cometchat-pro-react-native-ui-kit folder to your source folder.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61c596d66c9362600d81ac03_s_CD711F28ED418AC7E900AAB9DE0CB9B04B4580DCE3A553D5C1792000AD9D0F44_1637209140437_Screen%2BShot%2B2021-11-18%2Bat%2B11.18.36.png

Figure 28. Add the CometChat UI Kit to the React Native project.

  • Check your current package.json file, if your package.json file does not have the dependencies listed in the peer dependencies section in the image below, copy all the peer dependencies from the package.json, which is in the CometChat React Native UI Kit, into your project's package.json and install them using npm install.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61c596d510162e4cedd5c511_s_CD711F28ED418AC7E900AAB9DE0CB9B04B4580DCE3A553D5C1792000AD9D0F44_1637209174475_Screen%2BShot%2B2021-11-18%2Bat%2B11.19.27.png

Figure 29. Copy all peer dependencies and install them

As soon as the installation is completed, you now have access to all the React Native UI Components. The React UI kit contains different chat UI components as you can see in the documentation here. It includes:

1. CometChatUI

2. CometChatUserListWithMessages

3. CometChatGroupListWithMessages

4. CometChatConversationListWithMessages

5. CometChatMessages

6. CometChatUserList

7. CometChatGroupList

8. CometChatConversationList

9. CometChatAvatar.

10. CometChatUserPresence

11. CometChatBadgeCount

The Home Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/62f51edaa78172eb86dd92af_s_1FBE083E6A404BD576DEE9FADE73049F282CEBE22960A8ED45650A315C2C54FF_1652175417543_Screen%2BShot%2B2022-05-10%2Bat%2B4.36.32%2BPM.png

Figure 30. The Home Component

To show the full chat application, we need to use CometChat Kit. The CometChat team is handling everything for us, they are creating the CometChat SDK that contains the full-text chat, voice, and video calling functionalities. They also provide the CometChat React Native UI Kit, we just need to reuse the components from the UI Kit, and then we can build a full chat application with minimal effort. It means that we do not need to develop everything from scratch. What we need to do is very easy. We just need to import the CometChatUIKit component to get the full features. Please create the “home” folder inside the “components” folder and create the “Home.js” file inside it. The full source code can be found here.

Conclusion

To run the application, we can follow the guides which is mentioned here.

In conclusion, we have done an amazing job in developing a full chat application by leveraging React Native, Firebase, and CometChat UI Kit. You’ve been introduced to the chemistry behind the full chat application and how the CometChat makes the chat application buildable.

You have seen how to build most of the chat functionalities such as real-time messaging using CometChat. I hope you enjoyed this tutorial.

Hiep Le

CometChat

Hiep Le is a software engineer. He takes a huge interest in building software products and is a full-time software engineer. Most of his work is focused on one thing - to help people learn.

Try out CometChat in action

Experience CometChat's messaging with this interactive demo built with CometChat's UI kits and SDKs.