# Realtime DB by Firebase

## Realtime DB Overview

{% hint style="warning" %}
You should now use [cloud variables](https://docs.thunkable.com/snap-to-place/variables#app-stored-and-cloud-variables) instead of adding a Realtime DB component. This will work the same way in the background but will save you a lot of blocks.
{% endhint %}

{% hint style="danger" %}
We highly recommend you use your own Firebase account with your app. With the increased focus on user privacy, we strongly advise that you use own Firebase account both to ensure the privacy of your user data and to give you visibility into the database itself. We provide our database for your convenience but we may also clean out our shared account periodically
{% endhint %}

This storage option is useful for anything from saving game high scores to tracking work credit, powered by Firebase.

## Set up Your Own Firebase account

Thunkable provides a default Firebase account for testing purposes. When your app is ready to be distributed, we strongly advise connecting your app to your personal Firebase instance.

Firebase Realtime Database also pairs well with [Sign in](https://github.com/thunkable/thunkable-docs/tree/4a752596e288fca776105e94dc5e863bb9a3e25a/ios/components/screen-layout/authentication/sign-in.md) and you can use a single Firebase project for both services.

### Create your free [Firebase](https://firebase.google.com/) project and account

![](https://689802330-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAn5scXl2uqUJUOqkJo%2F-LAn5wecEraNWaG7Ig2g%2F-LAn69NBj53EC4ozJILb%2Ffirebase-%E2%9C%95-fig-3.png?generation=1524497166047443\&alt=media)

### Edit Database Rules

If you are using [Sign In](https://docs.thunkable.com/snap-to-place/sign-in-1) powered by Firebase in your app, you do not need to edit the default database rules which permit reading and writing to the database for authorized users.

![](https://689802330-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAn5scXl2uqUJUOqkJo%2F-LAn5wecEraNWaG7Ig2g%2F-LAn6HupxPPfNTxxbRfM%2Ffirebase-%E2%9C%95-fig-7.png?generation=1524497202436126\&alt=media)

Otherwise, you will need to set your Read and Write rules to `true`

![](https://689802330-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAn5scXl2uqUJUOqkJo%2F-LAn5wecEraNWaG7Ig2g%2F-LAn6HvwrNHk0Oi7n0Jk%2Ffirebase-%E2%9C%95-fig-8.png?generation=1524497202492322\&alt=media)

### Connect Your Firebase Project with Thunkable

To connect your Firebase project with Thunkable, you'll have to add an `API key` and `Database URL` to the Firebase Settings in the App Settings, which you can find by clicking on your app icon

![](https://689802330-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAn5scXl2uqUJUOqkJo%2F-LApqldWJ93l59nY1QAb%2F-LApsSuq5t2NdG5qVzLY%2FThunkable%20Documentation%20Exhibits%20\(67\).png?alt=media\&token=ed1970e6-e46b-4a57-83ab-28b555a986d8)

One way to retrieve the `API key` and `Database URL` from Firebase is to click on the `Add Firebase to your web app option`

![](https://689802330-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAn5scXl2uqUJUOqkJo%2F-LApqldWJ93l59nY1QAb%2F-LApscr-OTGTjmgIUEW8%2FThunkable%20Documentation%20Exhibits%20\(68\).png?alt=media\&token=64e70954-1dc4-4d83-b573-692adb7cf93d)

![](https://689802330-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAn5scXl2uqUJUOqkJo%2F-LApqldWJ93l59nY1QAb%2F-LApsexrEan1jVOMXVu3%2FThunkable%20Documentation%20Exhibits%20\(69\).png?alt=media\&token=02f1cfe8-af1f-4cd9-b0e0-b3ff80d22805)

### Test Save to your Database

Once your project is connected to your Thunkable app, we recommend that you do a simple test and save a value into your database. It should appear in your data tab in your Firebase console.

![](https://689802330-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAn5scXl2uqUJUOqkJo%2F-LAn5wecEraNWaG7Ig2g%2F-LAn6I-b-XX2NL4uwPct%2Ffirebase-%E2%9C%95-fig-10.png?generation=1524497202487564\&alt=media)

## Save Data

![](https://689802330-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAn5scXl2uqUJUOqkJo%2F-LAn5wecEraNWaG7Ig2g%2F-LAn6I1NCgG8jsqiziIe%2Ffirebase-%E2%9C%95-fig-11.png?generation=1524497202446222\&alt=media)

| Event                 | Description                                                 |
| --------------------- | ----------------------------------------------------------- |
| Save (`key`, `value`) | Asks Firebase to save a given `value` under the given `key` |

## Get Data

![](https://689802330-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAn5scXl2uqUJUOqkJo%2F-LAn5wecEraNWaG7Ig2g%2F-LAn6I5VNpVvRGsevbJY%2Ffirebase-%E2%9C%95-fig-12.png?generation=1524497202606423\&alt=media)

| Event               | Description                                                |
| ------------------- | ---------------------------------------------------------- |
| Get (`key`,`value`) | Asks Firebase to get the`value`stored under the given`key` |

## Update Data in Real-time

![](https://689802330-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAn5scXl2uqUJUOqkJo%2F-LAn5wecEraNWaG7Ig2g%2F-LAn6IAxsR6lSVxCQb22%2Ffirebase-%E2%9C%95-fig-13.png?generation=1524497202575491\&alt=media)

| Event                        | Description                                                                                             |
| ---------------------------- | ------------------------------------------------------------------------------------------------------- |
| Add Listener (`key`)         | Asks Firebase to listen to a specific`key`for changes in the database. Required for Data Changed block. |
| Data Changed (`key`,`value`) | Asks Firebase for updates to`value`for specified`key`. Requires an Add Listener block.                  |
| Remove Listener (`key`)      | Ask Firebase to stop listening to a specific`key`for changes in the database                            |

## Separating Your Data into Project Buckets

You might want to split your database into smaller spaces, also known as project buckets. To do this, you would need to save your value as an object and use the create object block to create your project bucket. More details in [this post](https://community.thunkable.com/t/project-bucket-option-in-ios/31957).

![Thanks to our power Thunker @actech for this awesome tip!](https://689802330-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAn5scXl2uqUJUOqkJo%2F-LFIsEzmNYj0c3VK07Uh%2F-LFIs_Zd-P7FVmLRPYF2%2Fimage.png?alt=media\&token=77d0cbe0-4e25-45fb-9221-31f5d510b4ea)
