Here's how I made a real-time chatbox in ReactJS and Firebase

Here's how I made a real-time chatbox in ReactJS and Firebase

 39
 2
 5 minutes

Cover Image

Hello! I recently learned using Firebase with ReactJS and thought of implementing my knowledge by creating some project. Since, I wanted to implement my firebase knowledge fast, I decided to make a very simple chat box instead of making very highly complex App which would take ages to complete.

In this post, I will share exactly how I made the live/real-time chatbox with Google OAuth. For this tutorial, we will not be using Context API or redux for holding state. We will be holding the state in the component's state.

Creating a new react App.


Installing necessary dependencies


Setting up a Firebase project

  1. Follow this Youtube tutorial to learn how to create a new firebase project.
  2. Copy the config from the firebase console and create a new file "firebase/config.js" and paste in it. It would look somthing like this. (You can also copy your config data into .env.local file and use it here.)

3.Now we just have to initialize and export some variables in this file. First of all, import firebase from firebase package and then initialise firebase app using . Also create and export two variables to initialize authentication and firestore. The final code would look like this.


Creating the App UI

For this project, we will be having a very similar app layout which you can see below. App Structure

  1. Create a components folder (inside src folder) and create two components, Chat.js and Message.js. Also create corresponding .css files. The folder structure should now look something like this. Folder structure
  2. Let's edit App.js file and write some actual code. In this file, we will be rendering components on the basis of whether user is logged in or not. If the user if logged in, then render the Chat component, else render the Login button in the view. Also, as mentioned earlier we will be storing user information into the component state.

3.Now let's edit the Chat.js and Message.js file to make the UI look complete.


4.Now let's edit the Message.js to finally complete our App UI.


Firebase (The fun part 🔥)

  1. Now we will be coding the authentication and message sending part. In our App.js file,import firebase from firebase package and db variable from the config file create a loginWithGoogle() function and attach it as an onClick listener to the button. Also we will use useEffect hook to run a function everytime when the page renders. This function will log us back into the google account on page refresh. The code for final App.js is something like this.

2.In our Chat component, let's add a useEffect() hook to read the firestore database on every change and take a snapshot of it so that we can retreive the new data in real-time.


3.Now our app is ready to recieve messages, and now let's create the messaging functionality. For that, let's create a sendMessage() function that will create a message object (only if input is not empty) and then add it to the firebase firestore db. It will also scroll the view accordingly down to the bottom after every message. Our final code in Chat component will look like this.


And now our app is finally ready to be published.

Note - All the .css files can be found in the GitHub repo mentioned below.

GitHub Repo 👇 https://github.com/shaan71845/live-chatbox

Live Demo 👇 https://live-chatbox-26e1b.web.app/