The Firebase Realtime Database stores and synchronizes data using a NoSQL cloud database. Data is synchronized across all clients in realtime, and remains available when your app goes offline.
Before You Begin
Before you can use Firebase Realtime Database, you need to:
Register your C++ project and configure it to use Firebase.
If your C++ project already uses Firebase, then it's already registered and configured for Firebase.
Add the Firebase C++ SDK to your C++ project.
Note that adding Firebase to your C++ project involves tasks both in the Firebase console and in your open C++ project (for example, you download Firebase config files from the console, then move them into your C++ project).
Create a Database
Navigate to the Realtime Database section of the Firebase console. You'll be prompted to select an existing Firebase project. Follow the database creation workflow.
Select a starting mode for your Firebase Security Rules:
- Test mode
Good for getting started with the mobile and web client libraries, but allows anyone to read and overwrite your data. After testing, make sure to review the Understand Firebase Realtime Database Rules section.
To get started with the web, iOS, or Android SDK, select test mode.
- Locked mode
Denies all reads and writes from mobile and web clients. Your authenticated application servers can still access your database.
Choose a region for the database. Depending on your choice of region, the database namespace will be of the form
<databaseName>.<region>.firebasedatabase.app. For more information, see select locations for your project.
When you enable Realtime Database, it also enables the API in the Cloud API Manager.
Create and Initialize firebase::App
Before you can access the Realtime Database, you'll need to create and initialize the
Include the header file for
firebase::App, passing the JNI environment and a
reference to the Java Activity as arguments:
app = ::firebase::App::Create(::firebase::AppOptions("APPLICATION NAME"), jni_env, activity);
app = ::firebase::App::Create(::firebase::AppOptions("APPLICATION NAME"));
Access the firebase::database::Database Class
is the entry point for the Firebase Realtime Database C++ SDK.
::firebase::database::Database *database = ::firebase::database::Database::GetInstance(app);
If you have chosen to use public access for your rules, you can proceed to the sections on saving and retrieving data.
Setting up Restricted Access
If you do not want to use public access you can add Firebase Authentication to your app to control access to the database.
Learn how to structure data for Realtime Database.
Prepare to launch your app:
- On desktop platforms (Windows, Mac, Linux), the Firebase C++ SDK uses REST to access your database. Because of this, you must declare the indexes you use with Query::OrderByChild() on desktop or your listeners will fail.
- The desktop workflow version of Realtime Database does not support offline or persistence.