Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect two different Firebase projects to a single common Firebase database?

Scenario:

We have two different projects in Firebase named: Customer and Merchant.

These two projects have been created in the Firebase Console so both have different project IDs and they can not be merged together into a single project. Each of them using Remote config feature from Firebase so both already have google-services.json configuration file in respective projects.

Now, we need to introduce realtime chat feature using Firebase and want one on one chat between customer and merchant. If we use Firebase, then it states that even though we create another project Chat, then it has another google-services.json configuration file, which will conflict with the existing configuration files in both the projects.

Meaning, if we take customer app then it already has one Firebase configuration. So how can I add another chat Firebase configuration in it?

This is what I've tried:

  1. Made another android project named Chat and added its respective Firebase config to it and then added it as a module in Customer Android project which already has it's own Firebase config. So, when I compiled that it considered the main app's that is Customer app config file as Firebase config. It is not considering chat module JSON config at all.

  2. In this approach, I tried to open up another Firebase instance programatically using the answer here. But when I try to push any message to chat Firebase database, the connection closes with message wrong project id because the Firebase instance is considering the id of the customer app.

With above approaches, it failed. Anyone has any idea how to proceed with this or is it possible or not?

like image 911
Abhishek Birdawade Avatar asked Sep 07 '16 07:09

Abhishek Birdawade


People also ask

Can two apps access same Firebase database?

Yes, it is! a single firebase database can be access by multiple app.. provided each of your apps use the same authentication details.

Can a Firebase project have multiple apps?

You're limited to one database per project. You'll need multiple projects to get multiple db instances. You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.

How many projects can I have on Firebase?

A Firebase project is a container for Firebase Apps across Apple, Android, and web. Firebase restricts the total number of Firebase Apps within a Firebase project to 30.


1 Answers

To connect to another Firebase Realtime Database from another project, you first need to initialize a FirebaseApp instance for that other Firebase project, and give it an identifier - in this case "secondary":

FirebaseOptions options = new FirebaseOptions.Builder()
   .setApplicationId("1:530266078999:android:481c4ecf3253701e") // Required for Analytics.
   .setApiKey("AIzaSyBRxOyIj5dJkKgAVPXRLYFkdZwh2Xxq51k") // Required for Auth.
   .setDatabaseUrl("https://project-1765055333176374514.firebaseio.com/") // Required for RTDB.
   .build();
FirebaseApp.initializeApp(this /* Context */, options, "secondary");

Then, you can access the database using the same client APIs, but this time specifying which project you want to access by passing the relevant FirebaseApp to FirebaseDatabase.getInstance():

// Retrieve my other app.
FirebaseApp app = FirebaseApp.getInstance("secondary");
// Get the database for the other app.
FirebaseDatabase secondaryDatabase = FirebaseDatabase.getInstance(app);

And if you need to access project's default Database use this:

FirebaseDatabase database = FirebaseDatabase.getInstance();

for more information check this article.

like image 197
Mohamed Nageh Avatar answered Sep 30 '22 22:09

Mohamed Nageh