Fireship · @fireship

I built a chat app in 7 minutes with React & Firebase

Uploaded September 23, 2020 · Length 8:53

I built a chat app in 7 minutes with React & Firebase

Summary

Build a basic chat app in 7 minutes with React & Firebase, including user authentication and real-time data updates.

  • react
  • firebase
  • chat app
  • real-time data
  • user authentication
  • javascript

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    Once upon a time, building a real-time chat app required a lot of carefully engineered code.

  2. 0:05

    Today, when you combine tools like React and Firebase, you can build a basic chat app from

  3. 0:09

    scratch in under 7 minutes. The app you'll build today is live on the internet, so feel free to

  4. 0:13

    check it out. Now, I already know what most of you are thinking. You'll go into the live demo,

  5. 0:16

    say some terrible things, and try to break the app by spamming it with a ton of messages.

  6. 0:20

    But we can get a lot done in 7 minutes. There's plenty of time to implement security logic,

  7. 0:24

    and we'll even write some backend code to ban a user for life if they violate our community

  8. 0:28

    guidelines, but most importantly you'll learn how to authenticate users with Firebase and

  9. 0:32

    how to work with real-time data in Firestore using React hooks.

  10. 0:36

    If you're new here, like and subscribe, and you can reference the code on GitHub.

  11. 0:39

    And if you want to go beyond the basics, check out this React Fire article by Jarrett Helton

  12. 0:43

    on FireShip.io.

  13. 0:44

    Before we get started, let's quickly take a look at what we're building today.

  14. 0:47

    It's a single group chat room that everybody can participate in, kind of like Super Chat

  15. 0:51

    on YouTube.

  16. 0:52

    To enter the chat, a user must log in with their Google account.

  17. 0:54

    Once logged in, they can post messages into the chat.

  18. 0:57

    Each message is a document in the Firestore database.

  19. 1:00

    It contains the text, user ID, and timestamp for that message.

  20. 1:03

    We make a query to the database for the most recent messages.

  21. 1:06

    Whenever the underlying data changes on the backend, the React app will update with the

  22. 1:10

    most recent messages on the frontend.

  23. 1:12

    It's very easy to implement, and by the end of this video, you'll have a great starting

  24. 1:15

    point for building really any type of real-time application.

  25. 1:18

    Let's go ahead and get started.

  26. 1:19

    Open up the terminal, and then I'll set a timer for 7 minutes.

  27. 1:23

    Run npx create react app to create an initial React project.

  28. 1:26

    code followed by the name of your app to open it in VS Code. We have two extra dependencies to

  29. 1:30

    install, Firebase and React Firebase hooks. Once installed, we can open up the app.js file,

  30. 1:36

    which is where we'll write all of our front-end code. Go ahead and delete the boilerplate code

  31. 1:39

    except for the app in the header. First, we'll import the Firebase SDK along with Firestore for

  32. 1:44

    our database and Auth for user authentication. Then we'll import a couple of hooks here to make

  33. 1:49

    it easier to work with Firebase in React. We'll call Firebase initialize app to identify our

  34. 1:53

    project, and then we'll make a reference to the Auth and Firestore SDKs as global variables.

  35. 1:58

    But we don't have a Firebase project yet, so let's create one by clicking Add Project from