Building a React Chat App with Firebase

In this guide, we will walk through the steps to create a simple chat application using React, Firebase, and styled-components. By the end of this guide, you will have a working chat application where users can sign in with their Google account, send messages, and see other online users.

Table of Contents

Prerequisites

Before starting, make sure you have the following installed:

  • Node.js and npm
  • A code editor (like Visual Studio Code)
  • A Firebase account

Step 1: Setup Your React App

  1. Create a new React app:
    npx create-react-app react-chat-app
    cd react-chat-app
  2. Install the necessary packages:
    npm install firebase react-firebase-hooks styled-components

Step 2: Setup Firebase

  1. Go to the Firebase Console and create a new project.
  2. Add a new web app to your Firebase project and copy the firebase config object.
  3. Initialize Firebase in your project. Create a firebase.js file and add the following code:
    import { initializeApp } from "firebase/app";
    import { getAuth } from "firebase/auth";
    import { getFirestore } from "firebase/firestore";
    
    const firebaseConfig = <YOUR-FIREBASE-CONFIG>;
    
    const app = initializeApp(firebaseConfig);
    const myAuth = getAuth(app);
    const myFireStore = getFirestore(app);
    
    export { myAuth, myFireStore };

Step 3: Create Components and Contexts

  1. Create a components directory in the src folder.

  2. Inside the components directory, create the following components:

  • ChatRoom.js
  • ChatMessage.js
  • SignIn.js
  • SignOut.js
  • UserList.js
  1. Create a utils directory in the src folder and add the following utility functions:
  • generateChannelId.js
  • createOrFindChannel.js
  1. Add the respective code for each component and utility function. (Refer to the refactored code provided in the previous conversation)

Step 4: Styling Your Components

In this step, you are free to choose any styling library or methodology that you are comfortable with. Some popular choices include styled-components, emotion, tailwindcss, or even plain CSS. The important part is to ensure that your components are styled in a way that enhances the user experience.

  1. Choose a styling library or methodology and install any necessary packages.
  2. Apply styles to your components. Make sure to consider responsiveness and usability.

Step 5: Implementing Chat Functionality

  1. In ChatRoom.js, implement the functionality to send and receive messages.
  2. Use useCollectionData from react-firebase-hooks to listen for real-time updates from Firestore.
  3. Implement the sendMessage function to add new messages to Firestore.

Step 6: Implementing User Authentication

  1. In SignIn.js, implement the Google Sign-In functionality.
  2. In SignOut.js, implement the Sign Out functionality.

Step 7: Implementing User List and Private Messages

  1. In UserList.js, display a list of online users.
  2. Implement the functionality to start a private chat with a selected user.

Step 8: Finishing Touches

  1. Add any additional styling or functionality as needed.
  2. Test the application to ensure everything is working as expected.

Conclusion

Congratulations! You have now built a simple React chat application using Firebase. You have implemented user authentication, real-time messaging, and the ability to start private chats with other users. Feel free to extend the functionality or add additional features to enhance the application further. Happy coding!