Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multiple instances of firebase-admin

I have multiple Firebase databases, and I would like to create one admin which connects to all databases. To initialize the app I first require the Firebase-admin module and initialize the app. If I run this again with different credentials it will still only initialize the one app.

var admin = require("firebase-admin");
...


Object.keys(config).map(function (k, i){

var c = config[k];

var serviceAccount = require(c.credential);

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    apiKey: c.apiKey,
    authDomain: c.authDomain,
    databaseURL: c.databaseURL,
    storageBucket: c.storageBucket
}, c.name);
...
}

This does not work! Only one app is initialized even though there are two configurations.

like image 593
Paul de Wit Avatar asked Nov 16 '16 06:11

Paul de Wit


People also ask

Can you have multiple Firebase projects?

However, when you want to access multiple projects from a single application, you'll need a distinct Firebase application object to reference each one individually. It's up to you to initialize these other instances.

How do I create a multiple Realtime Database in Firebase?

Create multiple Realtime Database instancesIn the Firebase console, go to the Data tab in the Develop > Database section. Select Create new database from the menu in the Realtime Database section. Customize your Database reference and Security rules, then click Got it.

Can two apps use same Firebase database?

Yes, You can use the same firebase database in more than one android application as below: In the Project Overview section of Firebase Console add an android application. For adding this application first you need to give that package name. Download config file in JSON format.

How many users can Firebase handle?

Queries with limited sorting and filtering functionality can be performed with the firebase database. Cloud firestore assures automatic scaling and can handle 1 million concurrent connections and 10,000 writes/second.


2 Answers

// Initialize the default app
firebase.initializeApp(defaultAppConfig);

// Initialize another app with a different config
var otherApp = firebase.initializeApp(otherAppConfig, "other");

console.log(defaultApp.name);  // "[DEFAULT]"
console.log(otherApp.name);    // "other"

// Use the shorthand notation to retrieve the default app's services
var defaultAuth = firebase.auth();
var defaultDatabase = firebase.database();

// Use the otherApp variable to retrieve the other app's services
var otherAuth = otherApp.auth();
var otherDatabase = otherApp.database();

Check out the Initialize mutltiple apps docs of the Firebase Admin SDK setup guide for more details.

like image 123
jwngr Avatar answered Oct 20 '22 13:10

jwngr


Figured it out...

var app = admin.initializeApp({... })

var db = admin.database(app);
like image 20
Paul de Wit Avatar answered Oct 20 '22 13:10

Paul de Wit