Firebase Realtime Database integration in mobile app

NOVASOLUTIONS.TECHNOLOGY is engaged in the development, support and maintenance of iOS, Android, PWA mobile applications. We have extensive experience and expertise in publishing mobile applications in popular markets like Google Play, App Store, Amazon, AppGallery and others.
Development and support of all types of mobile applications:
Information and entertainment mobile applications
News apps, games, reference guides, online catalogs, weather apps, fitness and health apps, travel apps, educational apps, social networks and messengers, quizzes, blogs and podcasts, forums, aggregators
E-commerce mobile applications
Online stores, B2B apps, marketplaces, online exchanges, cashback services, exchanges, dropshipping platforms, loyalty programs, food and goods delivery, payment systems.
Business process management mobile applications
CRM systems, ERP systems, project management, sales team tools, financial management, production management, logistics and delivery management, HR management, data monitoring systems
Electronic services mobile applications
Classified ads platforms, online schools, online cinemas, electronic service platforms, cashback platforms, video hosting, thematic portals, online booking and scheduling platforms, online trading platforms

These are just some of the types of mobile applications we work with, and each of them may have its own specific features and functionality, tailored to the specific needs and goals of the client.

Showing 1 of 1 servicesAll 1735 services
Firebase Realtime Database integration in mobile app
Medium
from 1 business day to 3 business days
FAQ
Our competencies:
Development stages
Latest works
  • image_mobile-applications_feedme_467_0.webp
    Development of a mobile application for FEEDME
    756
  • image_mobile-applications_xoomer_471_0.webp
    Development of a mobile application for XOOMER
    624
  • image_mobile-applications_rhl_428_0.webp
    Development of a mobile application for RHL
    1050
  • image_mobile-applications_zippy_411_0.webp
    Development of a mobile application for ZIPPY
    947
  • image_mobile-applications_affhome_429_0.webp
    Development of a mobile application for Affhome
    862
  • image_mobile-applications_flavors_409_0.webp
    Development of a mobile application for the FLAVORS company
    445

Integrating Firebase Realtime Database into Mobile Application

Firebase Realtime Database (RTDB) — JSON tree with WebSocket sync. Not relational, not document database in classic sense. Key feature — offline persistence and real-time sync out of box. But wrong data structure in RTDB turns these advantages into problem: subscribing to node with deep nesting will pull entire subtree into device memory.

Data Structure: Denormalization Mandatory

RTDB has no JOIN. If you want user posts — don't nest posts inside user. Denormalize:

{
  "users": {
    "uid123": { "name": "Ivan", "email": "ivan@..." }
  },
  "posts": {
    "postId1": { "userId": "uid123", "text": "...", "createdAt": 1700000000 }
  },
  "userPosts": {
    "uid123": { "postId1": true, "postId2": true }
  }
}

userPosts — reverse index to get specific user's posts without scanning entire posts node. Standard RTDB pattern.

Subscriptions in React Native

import database from '@react-native-firebase/database';

useEffect(() => {
  const ref = database().ref(`/userPosts/${userId}`);

  // value: full snapshot on each change
  const onValue = ref.on('value', snapshot => {
    const postIds = Object.keys(snapshot.val() ?? {});
    setPostIds(postIds);
  });

  // child_added: only new elements
  const onChildAdded = ref.on('child_added', snapshot => {
    setPostIds(prev => [...prev, snapshot.key!]);
  });

  return () => {
    ref.off('value', onValue);
    ref.off('child_added', onChildAdded);
  };
}, [userId]);

Critical: always call ref.off() on unmount. on() without off() — memory leak: listener lives forever, rerenders unmounted component. In production this crashes with Can't perform a React state update on an unmounted component.

Offline Persistence

// index.js, before any database() calls
import database from '@react-native-firebase/database';
database().setPersistenceEnabled(true);
database().setPersistenceCacheSizeBytes(10 * 1024 * 1024); // 10 MB

setPersistenceEnabled(true) enables SQLite cache on device. Offline, app reads from cache. On network recovery — syncs changes. Call only once at init, before first database access.

keepSynced(true) on specific node — pre-loads data and keeps in cache even without active listeners:

database().ref(`/userPosts/${userId}`).keepSynced(true);

Careful: don't apply keepSynced to large nodes — RTDB downloads entire tree.

Security Rules

Default RTDB rules — either everyone read/write or nobody. Must configure before production:

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    },
    "posts": {
      "$postId": {
        ".read": "auth != null",
        ".write": "auth != null && newData.child('userId').val() === auth.uid",
        ".validate": "newData.hasChildren(['userId', 'text', 'createdAt'])"
      }
    }
  }
}

.validate — checks data structure before write. Without validation, client can write arbitrary JSON.

Transactions for Concurrent Updates

Likes, counters, balance — any concurrent increment:

const likeRef = database().ref(`/posts/${postId}/likes`);
await likeRef.transaction(currentLikes => (currentLikes ?? 0) + 1);

transaction() atomically reads and writes. If another client changed between read and write — transaction repeats automatically (up to 25 times). For likes this is the only correct approach — set(currentLikes + 1) gives race condition on simultaneous taps.

RTDB vs Firestore: When to Choose What

RTDB better for: real-time chats, presence/online statuses, game leaderboards, event streams. Firestore better for: complex queries, document collections, scaling > 1M users.

RTDB cost: $5/GB storage + $1/GB traffic. High update frequency (chat, real-time) makes RTDB cheaper than Firestore due to no cross-billing by read/write operations.

Assessment

RTDB with offline persistence, security rules and real-time subscriptions: 2–3 weeks.