# RxDB - Firebase Realtime Database Alternative to Sync With Your Own Backend

> Looking for a Firebase Realtime Database alternative? RxDB offers a fully offline, vendor-agnostic NoSQL solution with advanced conflict resolution and multi-platform support.

# RxDB - The Firebase Realtime Database Alternative That Can Sync With Your Own Backend

Are you on the lookout for a **Firebase Realtime Database alternative** that gives you greater freedom, deeper offline capabilities, and allows you to seamlessly integrate with any backend? **RxDB** (Reactive Database) might be the perfect choice. This [local-first](./local-first-future.md), NoSQL data store runs entirely on the client while supporting real-time updates and robust syncing with any server environment—making it a strong contender against Firebase Realtime Database's limitations and potential vendor lock-in.

<center>
    
        
    
</center>

## Why RxDB Is an Excellent Firebase Realtime Database Alternative

### 1. Complete Offline-First Experience
Unlike Firebase Realtime Database, which relies on central infrastructure to process data, RxDB is fully embedded within your client application (including [browsers](./browser-database.md), [Node.js](../nodejs-database.md), [Electron](../electron-database.md), and [React Native](../react-native-database.md)). This design means your app stays completely functional offline, since all data reads and writes happen locally. When connectivity is restored, RxDB's syncing framework automatically reconciles local changes with your remote backend.

### 2. Freedom to Use Any Server or Cloud
While Firebase Realtime Database ties you into Google's ecosystem, RxDB allows you to choose any hosting environment. You can:
- Host your data on your own servers or private cloud.
- Integrate with relational databases like [PostgreSQL](../replication-http.md) or other NoSQL options such as [CouchDB](../replication-couchdb.md).
- Build custom endpoints using [REST](../replication-http.md), [GraphQL](../replication-graphql.md), or any other protocol.

This flexibility ensures you're not locked into a single vendor and can adapt your backend strategy as your project evolves.

### 3. Advanced Conflict Handling
Firebase Realtime Database typically updates data with a simple last-in-wins approach. RxDB, on the other hand, lets you implement more sophisticated conflict resolution logic. Using [revisions and conflict handlers](../transactions-conflicts-revisions.md#custom-conflict-handler), RxDB can merge concurrent edits or preserve multiple versions—ensuring your application remains consistent even when multiple clients modify the same data at the same time.

### 4. Lower Cloud Costs for Read-Heavy Apps
When you rely on Firebase Realtime Database, each query or listener can translate into ongoing reads, potentially running up your monthly bill. With RxDB, all queries are performed [locally](../offline-first.md). Your app only communicates with the backend to sync document changes, significantly reducing bandwidth and hosting expenses for applications that frequently read data.

### 5. Powerful Local Queries
If you've hit Firebase Realtime Database's querying limits, RxDB offers a far more robust approach to data retrieval. You can:
- Define custom indexes for faster local lookups.
- Perform sophisticated filters, joins, or full-text searches right on the client.
- Subscribe to real-time data updates through RxDB's [reactive query engine](../reactivity.md).

Because these operations happen locally, your [UI updates](./optimistic-ui.md) instantly, providing a snappy user experience.

### 6. True Offline Initialization
While Firebase offers some offline caching, it often requires an initial connection for authentication or to seed local data. RxDB, however, is built to handle an **offline-start** scenario. Users can begin working with the application immediately, regardless of connectivity, and any modifications they make will sync once the network is available again.

### 7. Works Everywhere JavaScript Runs
One of RxDB's core strengths is its ability to run in **any JavaScript environment**. Whether you're building a web app that uses IndexedDB in the browser, an [Electron](../electron-database.md) desktop program, or a [React Native](../react-native-database.md) mobile application, RxDB's **swappable storage** adapts to your runtime of choice. This consistency makes code-sharing and cross-platform development far simpler than being tied to a single backend system.

---

## How RxDB's Syncing Mechanism Operates

RxDB employs its own [Sync Engine](../replication.md) to manage data flow between your client and remote [servers](../rx-server.md). Replication revolves around:
1. **Pull**: Retrieving updated or newly created documents from the server.
2. **Push**: Sending local changes to the backend for persistence.
3. **Live Updates**: Continuously streaming changes to and from the backend for real-time synchronization.

## Sample Code: Sync RxDB With a Custom Endpoint

```ts
import { createRxDatabase } from 'rxdb/plugins/core';
import { getRxStorageLocalstorage } from 'rxdb/plugins/storage-localstorage';
import { replicateRxCollection } from 'rxdb/plugins/replication';

async function initDB() {
  const db = await createRxDatabase({
    name: 'localdb',
    storage: getRxStorageLocalstorage(),
    multiInstance: true,
    eventReduce: true
  });

  await db.addCollections({
    tasks: {
      schema: {
        title: 'task schema',
        version: 0,
        type: 'object',
        primaryKey: 'id',
        properties: {
          id: { type: 'string', maxLength: 100 },
          title: { type: 'string' },
          complete: { type: 'boolean' }
        }
      }
    }
  });

  // Start a custom replication
  replicateRxCollection({
    collection: db.tasks,
    replicationIdentifier: 'custom-tasks-api',
    push: {
      handler: async (docs) => {
        // post local changes to your server
        const resp = await fetch('https://yourapi.com/tasks/push', {
          method: 'POST',
          body: JSON.stringify({ changes: docs })
        });
        return await resp.json(); // return conflicting documents if any
      }
    },
    pull: {
      handler: async (lastCheckpoint, batchSize) => {
        // fetch new/updated items from your server
        const response = await fetch(
          `https://yourapi.com/tasks/pull?checkpoint=${JSON.stringify(
            lastCheckpoint
          )}&limit=${batchSize}`
        );
        return await response.json();
      }
    },
    live: true
  });

  return db;
}
```

### Setting Up P2P Replication Over WebRTC
In addition to using a centralized backend, RxDB supports peer-to-peer synchronization through WebRTC, enabling devices to share data directly.

```ts
import {
  replicateWebRTC,
  getConnectionHandlerSimplePeer
} from 'rxdb/plugins/replication-webrtc';

const webrtcPool = await replicateWebRTC({
  collection: db.tasks,
  topic: 'p2p-topic-123',
  connectionHandlerCreator: getConnectionHandlerSimplePeer({
    signalingServerUrl: 'wss://signaling.rxdb.info/',
    wrtc: require('node-datachannel/polyfill'),
    webSocketConstructor: require('ws').WebSocket
  })
});

webrtcPool.error$.subscribe((error) => {
  console.error('P2P error:', error);
});
```

Here, any client that joins the same topic communicates changes to other peers, all without requiring a traditional client-server model.

## Quick Steps to Get Started

1. Install RxDB
```bash
npm install rxdb rxjs
```

2. Create a Local Database
```ts
import { createRxDatabase } from 'rxdb/plugins/core';
import { getRxStorageLocalstorage } from 'rxdb/plugins/storage-localstorage';

const db = await createRxDatabase({
  name: 'myLocalDB',
  storage: getRxStorageLocalstorage()
});
Add a Collection
ts
Kopieren
await db.addCollections({
  notes: {
    schema: {
      title: 'notes schema',
      version: 0,
      primaryKey: 'id',
      type: 'object',
      properties: {
        id: { type: 'string', maxLenght: 100 },
        content: { type: 'string' }
      }
    }
  }
});
```

3. Synchronize

Use one of the [Replication Plugins](../replication.md) to connect with your preferred backend.

### Is RxDB the Right Solution for You?

- **Long Offline Use**: If your users need to work without an internet connection, RxDB's built-in offline-first design stands out compared to Firebase Realtime Database's partial offline approach.
- **Custom or Complex Queries**: RxDB lets you perform your [queries](../rx-query.md) locally, define [indexing](../rx-schema.md#indexes), and handle even complex [transformations](../rx-pipeline.md) locally - no extra call to an external API.
- **Avoid Vendor Lock-In**: If you anticipate needing to move or adapt your backend later, you can do so without rewriting how your client manages its data.
- **Peer-to-Peer Collaboration**: Whether you need quick demos or real production use, [WebRTC replication](../replication-webrtc.md) can link your users directly without central coordination of data storage.
