Learn how to set up Firebase in your project and read data from its Realtime Database on Android, iOS, and the web with our step-by-step guide.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set up Firebase in your project
Before you can read data from Firebase Realtime Database, you need to set up Firebase in your project. Follow these steps:
Step 2: Add Firebase Realtime Database to your app
Step 3: Add Firebase SDK to your app
For Android (Kotlin/Java):
Open your Android project and add the following dependencies to your build.gradle file:
```groovy
dependencies {
implementation platform('com.google.firebase:firebase-bom:31.2.3')
implementation 'com.google.firebase:firebase-database-ktx'
}
```
Sync your project with the Gradle files.
For iOS (Swift):
Open your Xcode project and add Firebase to your Podfile:
```ruby
pod 'Firebase/Database'
```
Run pod install in the terminal to install the Firebase SDK.
For Web (JavaScript):
Add Firebase to your project by including the Firebase JavaScript SDK:
```html
```
Step 4: Initialize Firebase in your app
In your app's entry point, initialize Firebase using the configuration details for your project.
For Android (Kotlin/Java):
import com.google.firebase.FirebaseApp
FirebaseApp.initializeApp(this)
For iOS (Swift):
import Firebase
FirebaseApp.configure()
For Web (JavaScript):
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
databaseURL: "https://YOUR_PROJECT_ID.firebaseio.com",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_PROJECT_ID.appspot.com",
messagingSenderId: "YOUR_SENDER_ID",
appId: "YOUR_APP_ID"
};
firebase.initializeApp(firebaseConfig)
Step 5: Read data from Firebase Realtime Database
Once Firebase is initialized, you can read data from the Realtime Database using the SDK.
For Android (Kotlin/Java):
val database = Firebase.database
val myRef = database.getReference("your_ref")
myRef.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val value = dataSnapshot.getValue(String::class.java)
println("Value is: $value")
}
override fun onCancelled(error: DatabaseError) {
println("Failed to read value. ${error.toException()}")
}
})
For iOS (Swift):
var ref: DatabaseReference!
ref = Database.database().reference()
ref.child("your_ref").observeSingleEvent(of: .value, with: { snapshot in
let value = snapshot.value as? String
print("Value is: \(value)")
}) { error in
print(error.localizedDescription)
}
For Web (JavaScript):
const database = firebase.database();
const ref = database.ref('your_ref');
ref.once('value').then((snapshot) => {
const value = snapshot.val();
console.log('Value is:', value);
}).catch((error) => {
console.error('Failed to read value:', error);
});
Step 6: Handle permissions and security
Make sure you configure your Firebase Realtime Database rules to allow read access to the data you wish to access, particularly during development. The Firebase Realtime Database has a security rules system that by default might prevent unauthorized access. Modify these rules as needed for your use-case:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
In development, you can temporarily set both read and write to true for testing purposes, but ensure proper authentication is implemented for production.
Step 7: Deploy and test
Once everything is set up, deploy your app to your test environment and observe the console to ensure data is being read correctly from the Firebase Realtime Database.
Make sure the data structure in your database matches what you are trying to access, and debug any issues based on the log output. Check permissions and connectivity if data is not being fetched as expected.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.