React UI Kit

How to Build a Discord Clone Chat App

In this tutorial, we are building a React Chat App that serves as a basic Discord Clone, allowing users to login, chat, audio and video call users, and invite others to join the channels.

Hiep Le • Nov 29, 2021

App and web development have come a long way over the last few years. We use a lot of chat apps every day. One of the most widely used features is live chat including text chat and voice chat such as Facebook, Discord, WhatsApp, and so on. Using the React CometChat UI Kit, Firebase backend services, you will learn how to build the Discord clone.

Follow along the steps to build a Discord clone that will allow users:

1. Users

  • A way for end-users to signup and sign out (email & password is sufficient)

  • Users to send, receive and accept/reject friend requests.

  • Users can invite other users to join the channels

2. Channels

  • Text Channels

- List of all text chat channels in sidebar

- Users can create channels.

- Users can send and receive messages in channels

- Please include basic chat features like emojis, GIFs, media sharing, mentions, threads

  • Voice Channel

  • Ability to create voice channels

  • Chat over voice and video

  • Screen Sharing

3. The ability for users to chat with friends directly via DM.

4. Login the logged-in user to CometChat.

5. Create a user-friendly and attractive UI like the Discord app.

Prerequisites

To follow this tutorial, you must have a degree of understanding of the general use of React.js. This will help you to improve your understanding of this tutorial.

Installing the App Dependencies

Step 1: You need to have Node.js installed on your machine.

Step 2: Create a new project with the name discord-clone by running the following statement.

Step 3: You need to install some dependencies such as CometChat, Firebase, Validator, Uuid.

*Note: At this time of writing this tutorial, we are using the Firebase SDK with the version @8.9.1

Configuring CometChat SDK

  1. Head to CometChat Dashboard and create an account.

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

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

Add real-time chat with minimal effort using CometChat

2. After registering a new account, you need to the 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

Log in to the CometChat Dashboard with your created account

3. From the dashboard, add a new app called "discord-clone".

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

Create a new CometChat app - Step 1

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

Create a new CometChat app - Step 2

4. Select this newly added app from the list.

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

Select your created app

5. From the Quick Start copy the APP_ID, REGION, and AUTH_KEY, which will be used later.

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

Copy the the APP_ID, REGION, and AUTH_KEY

6. Navigate to the Users tab, and delete all the default users (very important).

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

Navigate to Users tab and delete all the default users

7. Navigate to the Groups tab and delete all the default groups (very important).

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

Navigate to Group tab and delete all the default groups

8. Create a file called **.env** in the root folder of your project.

9. Import and inject your secret keys in the **.env** file containing your CometChat and Firebase in this manner.

10. Make sure to include **.env** in your gitIgnore file from being exposed online.

Setting Up Firebase Project

According to the requirements of the Discord clone, you need to let users create a new account and login to the application, Firebase will be used to achieve that. Head to Firebase to create a new project and activate the email and password authentication service. This is how you do it:

To begin using Firebase, you’ll need a Gmail account. Head over to Firebase and create a new project.

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

Firebase

Firebase provides support for authentication using different providers. For example, Social Auth, phone numbers, as well as the standard email and password method. Since you will be using the email and password authentication method in this tutorial, you need to enable this method for the project you created in Firebase, as it is by default disabled.

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

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

Firebase Authentication

Next, click the edit icon on the email/password provider and enable it.

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

Enable Firebase Authentication with Email and Password

Now, you need to go enable Firebase Realtime Database. We will Firebase Realtime Database to store the information of the users in the application. Please refer to the following part for more information.

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

Choose “Realtime Database” option

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

Click on “Create Database"

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

Select location where you realtime database will be stored

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

Select “Start in test mode” for the learning purpose

Please follow the guidance from Firebase. After following all steps, you will see the database URL. If you just need to update the “FIREBASE_REALTIME_DATABASE_URL” variable in your “Constants.java” file with that value.

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

Database URL

On the other hand, your Firebase real-time database will be expired in the future. To update the rules you just need to select the “Rules” tab and update the date/time in milliseconds as you can see in the image below.

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

Update Firebase Realtime Database Rules

The below images demonstrate the data structure of the application. A user should have an avatar, an email, full name, id, and list of friends. On the other hand, if the end-user has sent a friend request to another user, that request is stored in the waiting field. Aside from that, if someone also sent a friend request to that user, that request is stored in the pending field.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499b7cf8f4cd9ad4a59c6_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636811231257_Screen%2BShot%2B2021-11-13%2Bat%2B20.46.48.png

Data Structure - User

According to the requirements, the end-users can create the text/voice channels. The channel’s information should be stored in the Firebase Realtime Database. You can refer to the below images to understand the data structure of the text channels and the voice channels.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499b72e28d218466396d6_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636811485845_Screen%2BShot%2B2021-11-13%2Bat%2B20.50.05.png

Data Structure - Text Channel

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499b8a4fc1f6150ed702b_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636811517772_Screen%2BShot%2B2021-11-13%2Bat%2B20.51.48.png

Data Structure - Voice Channel

Configuring Styling for the Application

Inside your project structure, open the index.css files and paste the codes here. index.css file will contain all CSS of the application.

Configuring Images for the Application

In this project, we need to create a folder called “images” inside the root folder. The “images” folder is used to store all of the images that will be used to build the application. To get the images, you can refer to this link.

Creating React Context

In this application, we need to pass some data between the components. We can have several options to achieve that such as using state management libraries, RxJS, and so on. However, we can get in-built support from React by using the React Context API. the React Context API helps us pass data through the component tree without passing down at every level. Please create the “context.js” file inside the “src” folder. The full source code of the “context.js” file can be found here.

Initializing CometChat for the Application

The below codes initialize CometChat in your app before it spins up. The App.js file uses your CometChat API Credentials. We will get CometChat API Credentials from the .env file. Please do not share your secret keys on GitHub.

Actually, App.js does not contain only the above code. It also contains other business logic of the application. The full source code of App.js file can be found here.

Configuring the Firebase File

You need to create a “firebase.js” file inside the “src” folder and you need to enable Firebase realtime database. This file is responsible for interfacing with Firebase authentication and database services. Also, it makes ready our google authentication service provider enabling us to sign in with google. Secret keys will be stored in the .env file. As mentioned above, please do not share your secret keys on GitHub.

Project Structure

The image below reveals the project structure. Make sure you see the folder arrangement before proceeding.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499b86f2a9b296d2e2ac2_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636812033399_Screen%2BShot%2B2021-11-13%2Bat%2B21.00.13.png

Project Structure

Now, let's make the rest of the project components as seen in the image above.

The App.js File

The App.js file is responsible for rendering different components by the given routes. 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. On other hand, it will be used to initialize CometChat. The full source code of the App.js file can be found here.

The Loading Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499b8fa8533a6c035b689_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636814290477_Screen%2BShot%2B2021-11-13%2Bat%2B21.28.32.png

The Loading Component

The loading component will be shown when the system performs some side effects such as interacting with Firebase or calling CometChat APIs, and so on. This component will be used to increase user experience. If we do not have this component, the end-users cannot know when the data is loaded successfully. Please create the “components” folder inside the “src” folder, and create the “common” folder inside it. The “common” folder is used to store the common components that will be used in different places in your application. After that, you need to create the “Loading.js” file inside the “common” folder. The full source code of the loading component can be found here.

The Login Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499b9869ca54ec740bdd7_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636814328914_Screen%2BShot%2B2021-11-13%2Bat%2B21.38.41.png

The Login Component

This component is responsible for authenticating our users using the Firebase authentication service. It accepts the user credentials and either signs him up or in, depending on if he is new to our application. See the code below and observe how our app interacts with Firebase and the CometChat SDK. Please create the “login” folder inside the “components” folder, and create the “Login.js” file inside the “login” folder. The full source code can be found here.

We store the authenticated Information to the local storage for further usage such as getting that information in different places in the application, prevent the end-users come back to the login page after signing in to the application.

To login to the CometChat, we call the cometChat.login function, you can refer to the below code snippet for more information.

The above code indicates that we are using withModal as a higher-order component. This higher-order component will be used to reuse the code of showing and hiding the custom modal. In this case, we want to show the sign-up modal to let end-users register new accounts. We will discuss the sign-up component in the following section.

The withModal - Higher-Order Component

As mentioned above, we would like to show the SignUp component as a modal. Actually, we have multiple modals in the application, and we should avoid duplicating common functionalities such as showing/hiding the modals, and so on. To reuse the common logic, we can have several options. In this project, we will create a higher-order component called “withModal”. That higher-oder component helps us to avoid duplicating code and we can customize the UI for the modals. Please follow the below code snippet for more information. Please create the “Modal.js” file inside the common folder. The full source code can be found here.

The Sign Up Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499b9f5bc13e1004ca485_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636814378026_Screen%2BShot%2B2021-11-13%2Bat%2B21.39.25.png

The SignUp Component

The sign-up component will help end-users to register new accounts. This component will do two things. The first thing is to register new accounts on Firebase by using the Firebase authentication service. Aside from that, it also registers new accounts on CometChat by using the CometChat SDK. Please create the “register” folder inside the “components” folder, and create the “SignUp.js” file inside it. The full source code can be found here.

To create a new CometChat account, we call cometChat.createUser function. 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 UI kit, we need to add it in our project so that we can reference it. In this case, we are using React UI Kit v3.0. To do that, follow the next steps:

Step 1: Clone the CometChat React UI Kit Repository like so:

Step 2: Copy the folder of the CometChat Pro React UI Kit you just cloned into the src folder of your project:

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/6141c84b66cb6b6d54ae9e23_s_57BAB16DD57BF6545F90BB21B568B0BA327152F4F154C712CC6ED37A52FFEB22_1629534462841_image.png

Copy the cloned folder in to the src folder

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/6141c84c42650f5c6b2dcf89_s_57BAB16DD57BF6545F90BB21B568B0BA327152F4F154C712CC6ED37A52FFEB22_1629978656013_react-ui-kit-dependencies.png

React UI Kit Dependencies

Step 3: Copy all the dependencies from the package.json file of the CometChat Pro React UI Kit folder and paste them in the dependencies section of the package.json file of your project.

Step 4: Save the file and install the dependencies like so: npm install

As soon as the installation is completed, you now have access to all the React UI Components. The React UI kit contains different chat UI components for different purposes 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

The Chat Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/6141c84a7789163e0ec1db65_s_57BAB16DD57BF6545F90BB21B568B0BA327152F4F154C712CC6ED37A52FFEB22_1629528923807_image.png

CometChatMessages Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/6141c84c75abc20ad0e8e482_s_57BAB16DD57BF6545F90BB21B568B0BA327152F4F154C712CC6ED37A52FFEB22_1629529010288_image.png

CometChatMessages Component - Voice and Video Calling

The Left Menu Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499bac859ff5ba9f63d28_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636814669699_Screen%2BShot%2B2021-11-13%2Bat%2B21.43.32.png

The Left Menu Component

In this section, we will create the left menu component as you can see in the above image. To do that, we need to create the “Menus.js” file inside the “common” folder, we create that file inside the “common” folder because it is a common component, it will be reused in different places in the application.

The full source code of the “Menus.js” file can be found here.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499ba0d717c4f363413fb_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636815241835_Screen%2BShot%2B2021-11-13%2Bat%2B21.53.10.png

The Menu Item Component

In this tutorial, the list of menu items will not be changed. For this reason, we create a static list to store the information of each menu item, each item contains the id and icon. Each item is demonstrated by the Menu component. Please create the“Menu.js” file inside the “common” folder. The full source code of the Menu component can be found here. The Menu component accepts three props

  • isActive: indicates which menu has been selected, that one should be highlighted.

  • onItemSelected: attach the event for each menu item.

  • item: the item information that will be rendered on the UI.

You can refer to the below code snippet for more information.

The Friend Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499ba9215d91250ea75b2_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636815381927_Screen%2BShot%2B2021-11-13%2Bat%2B21.54.32.png

The Friend Component

According to the requirement, the end-users can do the following actions:

  • Users to send, receive and accept/reject friend requests.

  • The ability for users to chat with friends directly via DM

To achieve the above points, we need to build the Friend component. First of all, we create the “friend” folder inside the “components” folder. The “friend” folder is used to contain all components that help us build the “friend” features. The “friend” folder stores the below components:

  • <Add />

  • <Friend />

  • <Header />

  • <Main />

  • <NotFound />

  • <Pending />

  • <Pendings />

  • <RightSidebar />

  • <Search />

  • <Sidebar />

  • <Sidebars />

  • <SubHeader />

  • <User />

  • <Users />

All of the above components will be discussed in this section.

The <Friend /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499badfde32d4b148e446_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636815381927_Screen%2BShot%2B2021-11-13%2Bat%2B21.54.32.png

The Friend Component

The Friend component acts as the wrapper component, it contains other components inside it. You can imagine we are playing Lego and we are trying to connect different pieces to make the final result. Please create the “Friend.js” file inside the “friend” folder. The full source code of the Friend component can be found here.

As you can see from the above code snippet, the Friend component includes two components - Sidebars, and Main. We will create them in the following sections.

The <Sidebars /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499bbfa8533535435b716_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636817145923_Screen%2BShot%2B2021-11-13%2Bat%2B22.25.02.png

The Sidebars Component

The Sidebars component is used to render the list of friends. To build the Sidebar component, please create the “Sidebars.js” file inside the “friend” folder. The full source code of the Sidebars component can be found here.

After the friend request was accepted. The application sends a custom message to tell the sender about that. So the list of friends will be reloaded automatically on the sender side. To listen to the custom messages in real-time, we use the CometChat services. The best practice is to remove the listener if the component has been unmounted cometChat.removeMessageListener(user.id);. You can refer to the below code snippet for more information.

To increase the UX of the application, we will show/update the online/offline status of each friend in real-time. To achieve that, we need to use the CometChat service. You can refer to the below code snippet. Please do not forget to call cometChat.removeUserListener(userPresenseListenerId); to remove the listener after the component has been unmounted.

Each item on the Sidebars component will be demonstrated by the “Sidebar” component. We will create the Sidebar component in the following section.

The <Sidebar /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499bb809c0f0b124471bb_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636817512440_Screen%2BShot%2B2021-11-13%2Bat%2B22.31.03.png

The Sidebar Component

Please create the “Sidebar.js” file inside the “friend” folder. The Sidebar component accepts two props

  • Friend: indicates the information about each friend of the authenticated user.

  • onItemClicked: after the end-user has selected a friend, we need to show the chatbox. Therefore, we need to add the event handler for each item in the sidebar.

The full source code of the Sidebar component can be found here.

The <Main /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499bbc73d6b8068399ea4_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636817850197_Screen%2BShot%2B2021-11-13%2Bat%2B22.36.51.png

The Main Component

The main component will act as the wrapper component. It contains the <Header />, <CometChatMessage />, <Add />, <Pendings />, <RightSidebar /> components. Please do not worry, we will discuss those components in the below sections. The full source code of the main component can be found here.

The <Header /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a499bbe5b7b68f22568411_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636818111234_Screen%2BShot%2B2021-11-13%2Bat%2B22.41.11.png

The Header Component

As you can see in the above code snippet, the Main component contains the Header component. The Header component accepts two props:

  • selectedOption: the header item which was selected by the end-user.

  • onItemClicked: we attach the event handler for each item in the Header component so we can know which item would be selected by the end-user, and the application can perform the corresponding actions.

Please create the “Header.js” file inside the “friend” folder. The full source code of the Header component can be found here.

The <RightSidebar /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aa889389ebfe4455aaa_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636818397958_Screen%2BShot%2B2021-11-13%2Bat%2B22.45.59.png

The RightSidebar Component

The RightSidebar component takes the responsibility for rendering static information. Please create the “RightSidebar.js” file inside the “friend” folder. The full source code of the “RightSidebar” component can be found here.

The <NotFound /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aa8a4647176804012c8_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636818691965_Screen%2BShot%2B2021-11-13%2Bat%2B22.50.58.png

The NotFound Component

The NotFound component is used to render a funny image and increase the UX when there is no data to display. For example, if you do not have any pending requests so the NotFound component will be displayed instead of rendering a blank page. Please create the “NotFound.js” file inside the “friend” folder. The full source code of the NotFound component can be found here.

The <Add /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aa9ca74b301f72bab04_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636819291539_Screen%2BShot%2B2021-11-13%2Bat%2B22.59.08.png

The Add Component

As mentioned above, the end-users can send friend requests to other users in the Discord clone. Therefore, we have to provide UI to help the end-users can do that. Please create the “Add.js” file inside the “friend” folder. The full source code of the Add component can be found here. The Add component contains the <Search/>, <Users/>, <User/> components. On the other hand, it acts as the container component. It means that it will interact with the APIs, get the response data, and pass that data to the presentational components. Separating the business logic and the UI makes the code be easy to maintain, fix issues, and so on.

The <Search /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aa9c73d6b30d53a8fa8_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636819619434_Screen%2BShot%2B2021-11-13%2Bat%2B23.05.09.png

The Search Component

The Search component accepts one prop - onItemClicked. It means that whenever the end-users are typing something on the search box, we will send the input keywords to the Add component, the Add component takes those keywords, and then call the Firebase Realtime Database service for searching the corresponding users. Please create the “Search.js” file inside the “friend” folder. The full source code can be found here.

The <Users /> Component

After typing some keywords on the search box, we need to call the Firebase Realtime Database service to get the corresponding users and then render those users on the UI. The Users component will take responsibility for doing that. Please create the “Users.js” file inside the “friend” folder. The Users component accepts two props:

  • users: the list of users that will be rendered on the UI.

  • onConfirmShown: this is the callback function and it is triggered after the end-user has selected an item in the user’s list. The purpose of this function is to show the confirmation dialog to ensure that the end-users would like to send a friend request to the selected user.

The <User /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aa8514bea83ab1e5cf7_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636820072684_Screen%2BShot%2B2021-11-13%2Bat%2B23.14.02.png

The User Component

In the previous section, we have created the Users component. To render each user on the list, we use the User component, please create the “User.js” file inside the “friend” folder. The User component accepts two props:

  • user: the user that will be rendered on the UI.

  • onItemClicked: the callback function that will be triggered after the end-user has selected a user in the list.

So we can see here, the Users component takes the callback function and pass it further down to the User component. The full source code of the User component can be found here.

The <Pendings /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aa93ee3977d9affd0d8_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636820844134_Screen%2BShot%2B2021-11-13%2Bat%2B23.23.02.png

The Pendings Component

After we have received some add friend requests from other users, those requests will be shown on the UI by using the Pendings component. Please create the “Pendings.js” file inside the “friend” folder. The full source code of the Pendings component can be found here. As you can see in the above image, the Pendings component contains the SubHeader component and the Pending component. Aside from. that, the Pendings component performs as the container component, it will interact with the back-end services such as the Firebase Realtime Database service or the CometChat services, and so on. After getting the data from the back-end services, it will pass the data further down to its child. In this case, they are the SubHeader component and the Pending component.

After the pending request has been accepted, we need to call the CometChat API to make two users be friends. After that, as mentioned above, the application sends the custom message so the list of friends will be reloaded automatically on the sender side. You can refer to the below code snippet for more information. For more information about the CometChat API, you can refer to this link.

The <SubHeader /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aa9717d0d663257f7df_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636821139016_Screen%2BShot%2B2021-11-13%2Bat%2B23.31.31.png

The SubHeader Component

The SubHeader component accepts only one prop - the title. The purpose of the SubHeader component is to show the number of pending requests for the authenticated user. It would help us increase the UX for the application. Please create the “SubHeader.js” file inside the “friend” folder. The full source code can be found here.

The <Pending /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aaa272b1d1e507f785d_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636821289570_Screen%2BShot%2B2021-11-13%2Bat%2B23.34.15.png

The Pending Component

As mentioned above, the Pending component is used to render each pending request in the list. Please create the “Pending.js” file inside the “friend” folder. The Pending component accepts three props:

  • user: the user information which will be rendered in the list.

  • onAccepted: the callback function after the end-user has selected the “accept” button.

  • onRejected: the callback function after the end-user has selected the “reject” button.

After the end-user has selected the “accept”/”reject” button, we will update the data in the Firebase Realtime Database. The full source code can be found here.

The Server & Channel Component

According to the requirements, the end-users can do the following actions:

  • Users can invite other users and channels

  • List of all text chat channels in sidebar

  • Users can create channels.

  • Users can send and receive messages in channels

  • Include basic chat features like emojis, GIFs, media sharing, mentions, threads

  • Ability to create voice channels

  • Chat over voice and video

  • Screen Sharing

To do the above features, first of all, we need to create two folders inside the “components” folder - “channel” and “server”. The “channel” folder contains the components related to the channels, and the “server” folder contains the components related to the servers.

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aaadde6f5cd38c7640e_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636861325008_Screen%2BShot%2B2021-11-14%2Bat%2B10.41.08.png

The Server Component

The <Server /> Component

The server component acts as the wrapper component, it contains other components inside it. Please create the “Server.js” file inside the “server” component. The full source code of the Server component can be found here.

The <Sidebars /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aab3009e531e3069ee8_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636861656104_Screen%2BShot%2B2021-11-14%2Bat%2B10.46.54.png

We create the “Sidebars.js” file inside the “server” folder to create the Sidebars component. The Sidebars component shows the list of text/voice channels. Following that, it also contains the option that allows the end-users to create new channels. The full source code of the Sidebars component can be found here.

After someone has created a new channel, other users should know about that in real-time. To achieve that, the person, who created the channel, will send a custom message, other users listen to the custom messages in real-time, and reload the list of channels automatically. You can refer to the below code snippet for more information.

The <Channels /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aaa3e7fdf4ad81acbf5_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636862314830_Screen%2BShot%2B2021-11-14%2Bat%2B10.57.47.png

The Channels Component

As you can see from the above code snippet, the Sidebars component contains the Channels component to render the text channels and voice channels. Please create the “Channels.js” file inside the “channel” folder. The Channels component accepts three props:

  • title: The title of the channel.

  • channels: The list of channels.

  • channelType: The type of the channel - text channel or voice channel - 1 is text channel and 2 is voice channel.

  • onItemClicked: The callback function which would be attached to each channel, because after the end-user has selected a channel, we need to open the chatbox or start the direct call.

The full source code of the Channels component can be found here.

The <Channel /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aaaca74b35d5f2bab31_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636868470023_Screen%2BShot%2B2021-11-14%2Bat%2B12.40.31.png

The Channel Component

Each channel will be shown via the Channel component. The Channel component accepts three props:

  • channelType: the type of the channel.

  • channel: the information of the channel that will be rendered on the UI.

  • onItemClicked: the callback function that was attached to each channel. After the end-user has selected a channel, that callback function will be triggered.

Please create the “Channel.js” file inside the “channel” folder. The full source code of the Channel component can be found here.

The <Create /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aaa760e348af9cc1ada_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636868616818_Screen%2BShot%2B2021-11-14%2Bat%2B12.43.04.png

The Create Component

After the end-user has selected the plus icon on the left sidebar. The Create component will be displayed as a modal. Please create the “Create.js” file inside the “channel” folder. The full source code of the Create component can be found here.

We can understand each channel as a CometChat group. Hence, when we are talking about we are creating a new channel, it means that we are also creating a new CometChat group. To create a new CometChat group, we use cometChat.createGroup(group). You can refer to the below code snippet for more information.

The <Main /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aab91ced63b9c37c63a_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636869721047_Screen%2BShot%2B2021-11-14%2Bat%2B13.01.27.png

The Main Component

The Main component is used to render the chatbox if the end-user has selected a text channel or start a direct call if the end-user has selected a voice channel. On the other hand, the Main component also contains the RightSidebar component. After the end-user has selected a channel, the RightSidebar component will render the list of members on that channel. It would help us increase the UX for the application. Aside from that, the Main component has the Header component, the Header component does not display anything, it is created to make the UI be more attractive. Please create the “Main.js” file inside the “server” folder. The full source code of the Main component can be found here.

As mentioned above,  if the end-user has selected a voice channel, a direct call will be started. The CometChat service helps us to achieve that. For more information about direct calling, you can refer to this link.

The <Header /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aabe0ddcd48f706e936_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636872368413_Screen%2BShot%2B2021-11-14%2Bat%2B13.44.04.png

The Header Component

The Header does not display anything, it just has a border-bottom to make the UI be more attractive, it does not contain any business logic. Please create the “Header.js” file inside the “server” folder. The full source code can be found here.

The <RightSidebar /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aabe512b40b4be0be86_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636872387224_Screen%2BShot%2B2021-11-14%2Bat%2B13.44.31.png

The RightSidebar Component

After the end-user has selected a channel, the RightSidebar component shows the list of members that belong to that channel. Please create the “RightSidebar.js” file inside the “server” folder. The full source code of the RightSidebar component can be found here.

To load the list of members that belong to the selected channel, we can use the CometChat service because the channel could be understood as a CometChat group as mentioned earlier. You can refer to the code snippet below. If you would like to know more about other CometChat services, you can refer to this link.

On the other hand, the RightSidebar component has to reload the list of members if there is any change in the channel such as adding a new member, remove the current member, and so on. Fortunately, the CometChat services are providing a way to listen to the group changes in real-time. You can refer to the below code snippet for more information. The best practice is to remove the group listener after the component has been unmounted. We can achieve that by calling cometChat.removeGroupListener(selectedChannel.guid);

To increase the UX in the application, we also need to show the online/offline status of each user in the channel. We can listen to user presence by using the CometChat service. You can refer to the below code snippet. Please do not forget remove the user listener by calling cometChat.removeUserListener(userPresenseListenerId); after the component has been unmounted.

The <Sidebar /> Component

https://uploads-ssl.webflow.com/5f3c19f18169b62a0d0bf387/61a49aab9dc2028385003712_s_0185AE9A03B8423D592AE0B806315B1DBAEDE7BE6CA07BB9B5C8AD98D6DC3342_1636872426618_Screen%2BShot%2B2021-11-14%2Bat%2B13.45.15.png

The Sidebar Component

Each member will be rendered by using the Sidebar component. The Sidebar component accepts one props - the information of the member. Please create the “Sidebar.js” file inside the “server” folder. The full source code can be found here.

Wrapping Up

In conclusion, we have done an amazing job in developing a Discord clone by leveraging React.js, Firebase, CometChat SDK, and React UI Kit. You’ve been introduced to the chemistry behind Discord and how the CometChat SDK makes the Discord clone 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 and that you were able to successfully add mentions into your React chat app. Jump into the comments and let me know your experience.

About the Author

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.

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.