How to Build a Team Messenger Site With React (Slack Clone)
Tags: slack, cometchat, react, teammessenger
Published: 2021-06-22 11:15:04

What you’ll be building. [Demo](https://slack-clone-b1ecf.web.app/), Git Repo [Here](https://github.com/Daltonic/slack-clone).

![Slack Clone](https://paper-attachments.dropbox.com/s_AF3D8F2E79BF9CBAF0DFA6E04ABF6A1E25EEFCEA868D2CF45BEE4D0430BA95B8_1622387035796_slack-channel-msg.gif)

#### Introduction

Are you inspired enough as a developer? Are you starting your journey as a web developer? Or do you seek to improve your skills to the next level? If you said yes to any of the above questions, then this tutorial is for you. As a developer, you need to get your hands dirty and implement the best set of apps available in the market to get the right people interested in you. In this tutorial, we will be combining the full power of React, Firebase, and [CometChat](https://cometchat.com) to build a slack clone that will leave you mind-blown.


#### Prerequisites

To follow this tutorial, you must have a basic understanding of the rudimentary principles of [React](https://reactjs.org/). This will help you to speedily digest this tutorial.


#### Installing The App Dependencies

First, you need to have NodeJs installed on your machine; you can go to their [website](https://nodejs.org/) to do that.

Second, you need to also have the React-CLI installed on your computer using the command below.

```sh
   npm install -g create-react-app
```

Next, create a new project with the name slack-clone.

```sh
   npx create-react-app slack-clone
```

Now, install these essential dependencies for our project using the command below.
```sh
    npm install react-router-dom
    npm install @material-ui/core
    npm install @material-ui/icons
    npm install firebase
    npm install moment react-moment
    npm install moment-timezone
```

Now that we're done with the installations, let's move on to building our slack-clone solution.

#### Installing CometChat SDK
1. Head to [CometChat](https://app.cometchat.com/signup) Pro and create an account.
2. From the [dashboard](https://app.cometchat.com/apps), add a new app called "slack-clone".
3. Select this newly added app from the list.
4. From the Quick Start, copy the APP_ID, REGION and AUTH_KEY, which will be used later.
5. Also, copy the REST_API_KEY from the API & Auth Key tab.
6. Navigate to the Users tab, and delete all the default users and groups leaving it clean (very important).
7. Create a "app.config.js" in the src directory of the project.
8. Enter your secret keys from CometChat and Firebase below on the next heading.
9. Run the following command to install the CometChat SDK.

```sh
    npm install @cometchat-pro/chat@2.3.0 --save
```

#### The App Config File

The setup below spells out the format for configuring the **app.config.js** files for this project.

```js
    const firebaseConfig = {
        apiKey: 'xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx',
        authDomain: 'xxx-xxx-xxx-xxx-xxx-xxx-xxx',
        databaseURL: 'xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx',
        projectId: 'xxx-xxx-xxx',
        storageBucket: 'xxx-xxx-xxx-xxx-xxx',
        messagingSenderId: 'xxx-xxx-xxx',
        appId: 'xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx',
        measurementId: 'xxx-xxx-xxx',
    },
    
    const cometChat = {
      APP_ID: 'xxx-xxx-xxx',
      AUTH_KEY: 'xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx',
      REST_KEY: 'xxx-xxx-xxx-xxx-xxx-xxx-xxx-xxx',
      APP_REGION: 'xx',
    }
    
    export { firebaseConfig, cometChat }
```

#### Setting Up Firebase Project

Head to Firebase create a new project and activate the email and password authentication service.

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


![Firebase Console Create Project](https://paper-attachments.dropbox.com/s_94342B8C9D531180A1CAB38C7EF43DFCA121631072793ACD0C6A8FCD3626C584_1618991584708_screenshoteasy.png)


Firebase provides support for authentication using different providers. For example, Social Auth, phone numbers, as well as the standard email and password method. Since we’ll be using the email and password authentication method in this tutorial, we need to enable this method for the project we 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 Firebase currently supports.


![Firebase Authentication Options](https://paper-attachments.dropbox.com/s_755FF5EE7AE87EEDEE9D2B5DAF639F15198B42B3BC9D71E0D139561AF90E3306_1616326600809_firebaseauthentication.png)


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


![Firebase Enabling Authentication](https://paper-attachments.dropbox.com/s_755FF5EE7AE87EEDEE9D2B5DAF639F15198B42B3BC9D71E0D139561AF90E3306_1616326656064_authenticationsignin.png)


Now, 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.


![Youtube-Live Clone Project Page](https://paper-attachments.dropbox.com/s_94342B8C9D531180A1CAB38C7EF43DFCA121631072793ACD0C6A8FCD3626C584_1618991668979_screenshoteasy+1.png)


Once you’re done registering the application, you’ll be presented with a screen containing your application credentials. Take note of the second script tag as we’ll be using it shortly in our application.

Congratulations! Now that you're done with the installations, let's do some configurations.


#### Configuring CometChat SDK

Inside your project structure, open the **index.js & index.css** files and paste the codes below.
The above codes initialize CometChat in your app before it boots up. The index.js entry file uses your CometChat API Credentials. The app.config.js file also contains your Firebase Configurations variable file. Please do not share your secret keys on Github.


{% gist https://gist.github.com/Daltonic/871e80f70f11a06f3f5ae271bbab5850 %}


#### Configuring The Firebase File

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.


{% gist https://gist.github.com/Daltonic/3efd4dc0bf3c8f53758dbe201e33ba3c %}

#### Project Structure

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


![Slack Clone Project Structure](https://paper-attachments.dropbox.com/s_AF3D8F2E79BF9CBAF0DFA6E04ABF6A1E25EEFCEA868D2CF45BEE4D0430BA95B8_1622398255034_Screenshot+159.png)


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


#### The App Component

The App component is responsible for dynamically rendering our components employing the services of the **Auth-Guard**. The Auth-Guard ensures that only authenticated users are permitted to access our resources, thereby providing security for our application.

{% gist https://gist.github.com/Daltonic/b673fc4d4d5a0cb47115e0a4653da2cf %}

Replace everything in the **App.js and App.css** files with the above codes. Great, let’s move on to the next thing.


#### The Sub-Components

We are about to look at the various mini-components that complement the bigger components within our project. We will use the image to identify the various sub-components and what they do.


![](https://paper-attachments.dropbox.com/s_AF3D8F2E79BF9CBAF0DFA6E04ABF6A1E25EEFCEA868D2CF45BEE4D0430BA95B8_1622400764993_Screenshot+153_LI.jpg)


Each of the above components renders the following parts of the app. Yes, it's a well-styled react-reusable component. Let’s go ahead a spit out the codes that sponsor their individual operations.


#### The Header Component

Observe the amazing amount of CSS beautification within this component.

{% gist https://gist.github.com/Daltonic/235a9782b394870b5ed27cdaa09cea6a %}

#### The Sidebar Component

Observe the code carefully, you will definitely respect front-end development. Also, observe that this component employs the services of the **getChannel** and **getDirectMessages** methods on the initialization of this component. These records once retrieved are passed on to the sidebarOption component which then populates the sidebar view.


{% gist https://gist.github.com/Daltonic/1eda96862f889a6c7e920858e5ecf1ca %}


#### The SidebarOption Component

This reusable component solely functions as a navigational agent in our app. It keeps track of the user’s channel and also the online presence of a user’s friends.

{% gist https://gist.github.com/Daltonic/7fc90cfb164c97f20495de34bac103c4 %}

#### The Message Component

Lastly, the message component elegantly populates the view with a given list of messages either for a one-to-many or one-on-one chat.

{% gist https://gist.github.com/Daltonic/0a0325432487109b812058f084427b1b %}


At this point, we’re done with mentioning and explaining what the sub-components do. Let’s take a step further to the bigger components.

#### The Login Component

![The Login Component](https://paper-attachments.dropbox.com/s_AF3D8F2E79BF9CBAF0DFA6E04ABF6A1E25EEFCEA868D2CF45BEE4D0430BA95B8_1622440679523_Screenshot+150.png)


As elegant and simple as it looks, the login component features two major operations: sign up and sign in. Behind the scene, these two methods combine the power of Firebase auth-service and CometChat user authentications.

To illustrate this process, let’s consider a user called “James”. Now, James has to click on the green button that reads, “Sign in with Google”. If it's his first time in our system, it will register him on both Firebase and CometChat and also alerting him to sign in again.

Once the initial registration is achieved, the user can click on the green button once and be allowed to use our app with his google credentials. The codes below sponsor the operations of the login component.

{% gist https://gist.github.com/Daltonic/166c845d6645fe7e69a5e6713e8e6e04 %}

We’re done with the authentication procedure, let’s move on to the other pages of our application.

#### The Home Component

![The Home Component](https://paper-attachments.dropbox.com/s_AF3D8F2E79BF9CBAF0DFA6E04ABF6A1E25EEFCEA868D2CF45BEE4D0430BA95B8_1622443576733_Screenshot+151.png)


This component provides you with a warm welcome screen, giving you a first look at the beauty of the slack-clone. The codes are given below.


{% gist https://gist.github.com/Daltonic/d49beae4aa8ad8fa2fa87953565a3b52 %}


#### The Add Channel Component

![The Add Channel Component](https://paper-attachments.dropbox.com/s_AF3D8F2E79BF9CBAF0DFA6E04ABF6A1E25EEFCEA868D2CF45BEE4D0430BA95B8_1622443891866_Screenshot+152.png)


This component features a simple ability to create a new channel. A user is given the option to make the channel private or public which will determine how it is represented on the sidebar.

{% gist https://gist.github.com/Daltonic/835bb0fc22f598e5b603f9f771d851d5 %}


#### The Channel Component

![The Channel Component](https://paper-attachments.dropbox.com/s_AF3D8F2E79BF9CBAF0DFA6E04ABF6A1E25EEFCEA868D2CF45BEE4D0430BA95B8_1622442068819_Screenshot+160.png)


The channel component is responsible for a lot of things, including obtaining the channel details, getting the channel messages, listing the members, adding new members, and so on.

This component uses the Message sub-component to render messages on its view. It also features the ability to send new messages and view for incoming messages from other users concurrently using that channel with you. One more thing this component does is to allow users to call each other by means of a video call.


![Calling functionality](https://paper-attachments.dropbox.com/s_AF3D8F2E79BF9CBAF0DFA6E04ABF6A1E25EEFCEA868D2CF45BEE4D0430BA95B8_1622442735728_Screenshot+158.png)


It's a lot easier to disclose the codes responsible for all the actions associated with the channel component.

{% gist https://gist.github.com/Daltonic/f6b07fea07a4176a4fd9730f7973a25b %}

#### The User Component

![The User Component](https://paper-attachments.dropbox.com/s_AF3D8F2E79BF9CBAF0DFA6E04ABF6A1E25EEFCEA868D2CF45BEE4D0430BA95B8_1622443051429_Screenshot+157.png)

Still, the user component behaves the same way as the channel component but with some variations in features. With this component, you can search for friends and also have direct messages with them as given below.

{% gist https://gist.github.com/Daltonic/a525bae94068c59dfb2b02535ff968e8 %}

Congratulations on completing the **slack-clone**, now we have to spin up our application with the command below using our terminal.

```sh
npm start
```

#### Conclusion

In conclusion, we have done an epic job in the realm of software development. You’ve been introduced to the inner workings of slack and its messaging abilities using the CometChat SDK and Firebase.

You have seen firsthand how to integrate most of the CometChat functionalities such as texting and video chatting. It's time to rise and start crushing other kinds of applications with the values you have gotten from this tutorial.

#### About Author

Gospel Darlington is a remote full-stack web developer, prolific in Frontend and API development. He takes a huge interest in the development of high-grade and responsive web applications. He is currently exploring new techniques for improving progressive web applications (PWA). Gospel Darlington currently works as a freelancer and spends his free time coaching young people on how to become successful in life. His hobbies include inventing new recipes, book writing, songwriting, and singing. You can reach me on [LinkedIn](https://www.linkedin.com/in/darlington-gospel-aa626b125/), [Twitter](https://twitter.com/idaltonic), or [Facebook](https://www.facebook.com/darlington.gospel01).