---
title: Offline Support
description: >-
  Realtime Database offline persistence: setPersistenceEnabled(),
  goOffline()/goOnline(), setPersistenceCacheSizeBytes(), keepSynced(), and
  getServerTime().
next: /database/presence-detection
previous: /database/usage
---
> ## Documentation Index
> Fetch the complete documentation index at: https://rnfirebase.io/llms.txt
> Use this file to discover all available pages before exploring further.

## Prerequisites

- `@react-native-firebase/app` installed and native config completed per [Getting Started](/) (Android `google-services.json`, iOS `GoogleService-Info.plist`, Expo config plugin, or React Native CLI setup). From v26, New Architecture is required for packages with a native bridge.
- `@react-native-firebase/database` installed: `npm install --save @react-native-firebase/database` or `yarn add @react-native-firebase/database`, then rebuild native apps (`cd ios && pod install` on iOS).
- Call `setPersistenceEnabled` as early as possible in app startup, before other Database reads or writes in that process.

The Realtime Database provides support for offline environments. While the app is running, the native Firebase SDKs serve
reads and queue writes from a local cache. Enable disk persistence to keep that data across app restarts.

# Enabling Persistence

Persistence is disabled by default when using Realtime Database. Enable it by calling `setPersistenceEnabled` as early on in your application code as possible. `setPersistenceEnabled` configures the native iOS/Android SDK persistence layer (there is no standalone modular firebase-js-sdk equivalent):

```js
// index.js
import { AppRegistry } from 'react-native';
import { getDatabase, setPersistenceEnabled } from '@react-native-firebase/database';

setPersistenceEnabled(getDatabase(), true);

function App() {
  return null;
}

AppRegistry.registerComponent('app', () => App);
```

# Going offline

The API provides a `goOffline` function to force the Realtime Database SDK to go offline, which can be useful for testing.

```js
import { getDatabase, goOffline } from '@react-native-firebase/database';

goOffline(getDatabase());
```

Once offline, all operations will continue to execute, instead using a local instance of your database to perform writes to.
For example, we write to a record which has a listener whilst offline allowing the listener to be called with the updated data:

```jsx
import { useEffect } from 'react';
import { getDatabase, ref, onValue, goOffline, set } from '@react-native-firebase/database';

function App() {
  useEffect(() => {
    const db = getDatabase();
    const userAgeRef = ref(db, '/users/123/age');

    const unsubscribe = onValue(userAgeRef, snapshot => {
      console.log('Users age: ', snapshot.val());
    });

    goOffline(db);
    set(userAgeRef, 32).then(() => {
      console.log('User updated whilst offline.');
    });

    return () => unsubscribe();
  }, []);

  return null;
}
```

The above code will first execute the `onValue` listener with data from the remote database.

Once offline, `set` on the reference node will `locally` be set to a new value.

The `onValue` listener
however will now subscribe to the local database and provide the new value.

This provides the ability to write code which works in both an online and offline environment without worrying about
data synchronization.

# Going online

The `goOnline` function re-connects the Realtime Database with the remote database. Any locally written changes performed
whilst offline will be automatically synchronized with the remote database.

```js
import { getDatabase, goOnline } from '@react-native-firebase/database';

goOnline(getDatabase());
```

# Local persistence size

By default Firebase Database will use up to `10MB` of disk space to cache data. If the cache grows beyond this size,
Firebase Database will start removing data that hasn't been recently used. If you find that your application caches too
little or too much data, call `setPersistenceCacheSizeBytes` to update the default cache size. Valid range is 1 MB to 100 MB; call it before other database references are used. Like `setPersistenceEnabled`, this is a React Native Firebase helper with no standalone modular firebase-js-sdk equivalent:

```js
import { getDatabase, setPersistenceCacheSizeBytes } from '@react-native-firebase/database';

setPersistenceCacheSizeBytes(getDatabase(), 2000000); // 2MB
```

# Keeping a location synced

`keepSynced` tells the native SDK to keep a location (or query) synchronized even when there are no active listeners. The firebase-js-sdk modular package does not export a standalone `keepSynced()` helper.

```js
import { getDatabase, ref, keepSynced } from '@react-native-firebase/database';

async function syncScores() {
  const scoresRef = ref(getDatabase(), '/scores');
  await keepSynced(scoresRef, true);
  console.log('Scores location kept synced.');
}
```

# Server time

`getServerTime` returns a `Date` adjusted with the native `.info/serverTimeOffset` listener maintained by the database module. There is no equivalent modular helper in the firebase-js-sdk.

```js
import { getDatabase, getServerTime } from '@react-native-firebase/database';

const serverNow = getServerTime(getDatabase());
console.log('Estimated server time:', serverNow.toISOString());
```

To read the raw millisecond offset yourself with `onValue` on `/.info/serverTimeOffset`, see [Presence Detection](/database/presence-detection#server-time-offset).
