Writing Data to Firebase Realtime Database

Writing Simple Data

To write data to Firebase Realtime Database, use the set method. Here’s an example of writing user data to the database:

function writeUserData(userId, name, email) {
firebase.database().ref('users/' + userId).set({
username: name,
email: email
});
}

// Example usage
writeUserData('1', 'John Doe', 'john.doe@example.com');

Explanation: The `writeUserData` function writes user data to the Firebase Realtime Database by creating or updating a user entry with the given `userId`, `name`, and `email` under the path `users/userId`. For example, calling `writeUserData(‘1’, ‘John Doe’, ‘john.doe@example.com’)` will store the username and email for the user with ID ‘1’.

Writing Data with Push

If we want Firebase to generate a unique key for each new child, use the push method. This is useful for lists of data, like messages or tasks.

function addMessage(userId, message) {
const messageRef = firebase.database().ref('messages/').push();
messageRef.set({
userId: userId,
message: message
});
}

// Example usage
addMessage('1', 'Hello, World!');

Output Example

After running the above code, our database structure will look like this:

{
"users": {
"1": {
"username": "John Doe",
"email": "john.doe@example.com"
}
},
"messages": {
"-Mabcd1234": {
"userId": "1",
"message": "Hello, World!"
}
}
}

Explanation:The `addMessage` function adds a new message to the Firebase Realtime Database by generating a unique key for each message and storing the `userId` and `message` under the path `messages/`. For example, calling `addMessage(‘1’, ‘Hello, World!’)` will add a new message from user ‘1’ with the content ‘Hello, World!’.

Firebase Realtime Database: Reading and Writing Data

Firebase Realtime Database, a cloud-hosted NoSQL database developed by Google, provides a robust solution for achieving seamless real-time data updates across connected clients.

In this article, We will learn about the Firebase Realtime Database, How to Setting Up the Firebase Realtime Database, write data or read data to Firebase Realtime Database with an example of a Real-Time Simple Chat Application.

Similar Reads

What is Firebase Realtime Database?

Firebase Realtime Database is a cloud-hosted NoSQL database provided by Firebase which is a platform developed by Google for creating mobile and web applications. It enables real-time data synchronization across all clients connected to the database. When data changes, the connected clients receive updates immediately. The database stores data as JSON (JavaScript Object Notation) objects which allows for a flexible and hierarchical data structure. Firebase Realtime Database is known for its simple setup and ease of use also making it accessible for developers of varying skill levels....

Setting Up Firebase Realtime Database

Before we understanding the reading and writing data, let’s set up Firebase Realtime Database....

Writing Data to Firebase Realtime Database

Writing Simple Data...

Reading Data from Firebase Realtime Database

Reading Data Once...

Example: Real-Time Simple Chat Application

Let’s create a simple real-time chat application to demonstrate Firebase Realtime Database in action....

Conclusion

Overall, By following the steps outlined in this guide, developers can effectively initialize Firebase, manage data through write and read operations, and implement real-time data updates to create engaging user experiences. Whether you are building a chat application, a collaborative tool, or a real-time analytics dashboard, Firebase Realtime Database provides the essential infrastructure to achieve your development goals efficiently....

Contact Us