1

I have a web-app at work on web (with react.js), for which recently firebase sharding has been implemented on the BE systems.

Now my task is to read the data from multiple DB urls ( they can be 3 different ones for now)

so after discussion with one senior-dev, we implemented

// FirebaseClient.js file
constructor(appName = 'liveApp') {
        const findApp = firebase.apps.find((app) => app.name === appName);
        this.app = findApp || firebase.initializeApp(firebaseConfig, appName);
        this.user = null;
        this._dataRefs = {};
        this._typeBaseData = {};
        this._emitter = new EventEmitter();
        this.databaseConnectionPool = {};
        this.database = null;
        this.databaseEndpoints = {};
    }

setDatabaseConnection(endpoint) {
        if (!this.databaseConnectionPool[endpoint]) {
            this.databaseConnectionPool[endpoint] = this.app.database(endpoint);
        }

        return this.databaseConnectionPool[endpoint];
    }
getDatabaseConnection(eventId) {
        let endpoint = this.databaseEndpoints[eventId];

        if (!endpoint) {
            endpoint = firebaseConfig.databaseURL;
        }

        if (!this.databaseConnectionPool[endpoint]) {
            this.setDatabaseConnection(endpoint);
        }
        this.database = this.databaseConnectionPool[endpoint];
        return this.databaseConnectionPool[endpoint];
    }

    setIdMapping(eventId, endpoint) {
        if (!this.DatabaseEndpoints[eventId])
            this.DatabaseEndpoints[eventId] = endpoint;
    }

ref(eventId, type) {
        return this.getDatabaseConnection(eventId).ref(`${type}`);
        // return this.database.ref(`${type}`); // firebase.database(this.app).ref(`${type}`);
    }

and it worked fine on DEV environments, we even deployed and tested as well,

but when we deployed on production, I got this error (in image)

it says basically that there are multiple database being initialized.

So how do I get control over that?

enter image description here

initially I tried maintaining multiple firebase clients on my code, but then, firebaseClients wouldn't get ready when/before I want to render things , which caused issues.

I have also refered to this threads :

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Divyesh Parmar
  • 321
  • 1
  • 6
  • 15
  • There's a lot missing here, so it's gonna be hard to help. In general: You can't reinitialize the same `FirebaseApp` instance, as your code tries to do. You will need to initialize a (new) `FirebaseApp` once you know the URL of the database shard the client needs to connect to. – Frank van Puffelen Dec 30 '20 at 20:35
  • Should I mention the whole flow? The firebaseclient.js file shows whole client, class based with custom methods. There is a hook useFireabaseClient.js which basically initialize the client by `new FirebaseClient();` then authenticates and returns the client. This hook I call in my app-level context, allowing me to use it inside each row of table. These rows should be fetching data from their respective Firebase DB. I am understanding what you said, I tried approach to initialise multiple clients, but then by the time components render, clients were not ready. crashing the whole app. @frank – Divyesh Parmar Dec 31 '20 at 06:42
  • All I know is what the error tells me: you seem to call ` firebase.initializeApp(firebaseConfig, appName)` multiple times with the same `appName` and a different database URL in the `firebaseConfig` variable. – Frank van Puffelen Dec 31 '20 at 15:59

0 Answers0