/firebase-tutorials

How to write data in Realtime Database?

Learn how to set up your Firebase project, install the SDK, initialize your app, and write data to the Realtime Database with our easy step-by-step guide.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to write data in Realtime Database?

 

Step 1: Set Up Firebase Project

 

  1. First, go to the Firebase Console at https://console.firebase.google.com/.
  2. Click on Add Project.
  3. Enter a project name and click Continue.
  4. Disable Google Analytics for this project if you don't need it, and then click Create Project.
  5. After the project is created, click Continue.

 

Step 2: Install Firebase SDK in Your Project

 

  • If you are using npm for a Node.js project, run:

    npm install firebase
  • For a web project using a <script> tag, add:

    
    <script src="https://www.gstatic.com/firebasejs/9.x.x/firebase-app.js"></script>
    <script src="https://www.gstatic.com/firebasejs/9.x.x/firebase-database.js"></script>
    

 

Step 3: Initialize Firebase in Your Application

 

  • Add the following code snippet to initialize Firebase. Replace the placeholder values with your Firebase project's actual configuration details from the Firebase Console:

    
    import { initializeApp } from "firebase/app";
    import { getDatabase } from "firebase/database";
    
    const firebaseConfig = {
      apiKey: "YOUR_API_KEY",
      authDomain: "YOUR_AUTH_DOMAIN",
      databaseURL: "YOUR_DATABASE_URL",
      projectId: "YOUR_PROJECT_ID",
      storageBucket: "YOUR_STORAGE_BUCKET",
      messagingSenderId: "YOUR_MESSAGING_SENDER\_ID",
      appId: "YOUR_APP_ID",
    };
    
    const app = initializeApp(firebaseConfig);
    const database = getDatabase(app);
    

 

Step 4: Write Data to Realtime Database

 

  • To write data, use the set() method. Here's an example of writing an object to the database:

    
    import { ref, set } from "firebase/database";
    
    function writeUserData(userId, name, email, imageUrl) {
      set(ref(database, 'users/' + userId), {
        username: name,
        email: email,
        profile\_picture: imageUrl
      });
    }
    
    // Call the function
    writeUserData('1', 'John Doe', '[email protected]', 'https://example.com/johndoe/profile.jpg');
    

 

Step 5: Enable Realtime Database in Firebase Console

 

  1. Go back to your project in the Firebase Console.
  2. In the left sidebar, click on Realtime Database.
  3. Click Create Database.
  4. Select Start in Test Mode and click Next. Note: Test Mode allows read and write access to your database. Configure rules based on your security needs later.
  5. Click Done to finish setting up the database.

 

Step 6: Test Your Setup

 

  • Run your application and confirm that the data is being written to your Firebase Realtime Database.

  • Check the Firebase Console under the Realtime Database section to see if the data appears as expected.

 

Ensure your application can connect to the database, and the data structure is correctly written. Customize the data structure as needed for your specific application.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022