Building Real-Time Apps with React and Firebase

In today’s fast-paced world, real-time apps have become a necessity for businesses and users alike. Real-time apps provide a seamless user experience, enabling users to get updates instantly without having to refresh the page manually. One of the most popular frameworks for building real-time apps is React, and Firebase is a powerful backend platform that provides real-time data synchronization.

In this article, we’ll take a look at how to build real-time apps with React and Firebase.

What is React?

React is an open-source JavaScript library for building user interfaces. It was developed by Facebook and is used by many well-known companies like Instagram, Netflix, and Airbnb. React allows developers to build reusable UI components that can be easily composed to create complex user interfaces.

What is Firebase?

Firebase is a mobile and web application development platform. It provides various services like authentication, real-time database, hosting, and storage. Firebase allows developers to build serverless applications that scale easily and provide real-time data synchronization.

Setting up Firebase

Before we dive into building a real-time app, we need to set up Firebase. Follow the steps below to set up Firebase:

  1. Create a Firebase account
  2. Create a new project
  3. Add a new web app to the project
  4. Copy the Firebase configuration details

Building a real-time app with React and Firebase

Now that we have set up Firebase, we can start building our real-time app. In this example, we will build a simple chat application that allows users to send messages in real-time.

Step 1: Create a new React app

To create a new React app, open your terminal and run the following command:

npx create-react-app firebase-chat

This command creates a new React app called “firebase-chat” in your current directory.

Step 2: Install Firebase

To install Firebase, run the following command in your terminal:

npm install firebase

Step 3: Configure Firebase

In the “src” directory of your React app, create a new file called “firebase.js”. In this file, paste the Firebase configuration details that you copied earlier.

import firebase from 'firebase/app';
import 'firebase/database';

const firebaseConfig = {
  // Paste your Firebase configuration details here


export default firebase;

Step 4: Create a chat component

In the “src” directory of your React app, create a new file called “Chat.js”. In this file, we’ll create a new component called “Chat” that displays the chat messages.

import React, { useEffect, useState } from 'react';
import firebase from './firebase';

function Chat() {
  const [messages, setMessages] = useState([]);

  useEffect(() => {
    const messagesRef = firebase.database().ref('messages');
    messagesRef.on('value', (snapshot) => {
      const messages = snapshot.val();
      const messageList = [];
      for (let id in messages) {
  }, []);

  return (
        { => (
          <li key={}>{message.text}</li>

export default Chat;

Step 5: Create a form component

In the “src” directory of your React app, create a new file called “Form.js”. In this file, we’ll create a new component called “Form” that allows users to send messages.

import React, { useState } from 'react';
import firebase from './firebase';

function Form() {
  const [text, setText] = useState('');

  const handleSubmit = (event) => {
    const messagesRef = firebase.database().ref('messages');
    const message = {
      text: text,

  return (
    <form onSubmit={handleSubmit}>
        placeholder="Enter message"
        onChange={(event) => setText(}
      <button type="submit">Send</button>

export default Form;

Step 6: Add the components to the app

In the “App.js” file, we’ll add the “Chat” and “Form” components to our app.

import React from 'react';
import Chat from './Chat';
import Form from './Form';

function App() {
  return (
      <Chat />
      <Form />

export default App;

Step 7: Run the app

Finally, run the app using the following command:

npm start

The app should now be running on http://localhost:3000, and you should be able to send and receive messages in real-time!


Building real-time apps with React and Firebase is a powerful combination that can provide a seamless user experience. In this article, we learned how to set up Firebase, create a chat app with real-time messaging, and display the messages using React components. With the power of Firebase’s real-time data synchronization, you can create complex and scalable applications that provide real-time updates to your users.