Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Firebases instances

I'm having a problem when I try to make more than one new Firebase(url); instance.

For example:

var token1 = "c.AdASdsAsds...";
var token2 = "c.dkasdEddss...";

var v1 = new Firebase('https://developer-api.nest.com');
v1.auth(token1,function(err){ 
    console.log("Connected 1 "+err);
},function(err){ console.log("Cancel 1: "+err); });

var v2 = new Firebase('https://developer-api.nest.com');
v2.auth(token2,function(err){ 
    console.log("Connected 2 "+err);
},function(err){ console.log("Cancel 2 "+err); });

Console logs: Connected 2 null and that's it.. no more.

So, it means it never calls the callback function from v1.auth(); it ignores it, it looks like it becomes overridden by the v2.auth(); even though they are different Firebases instances, and it interferes everything else, for example, the v1.child("path").on("value",function(snapshot){}); and v2.child("path").on("value",function(snapshot){}); won't work when this happens.

like image 435
Carlos C Avatar asked Aug 19 '14 02:08

Carlos C


People also ask

Can you have multiple Firebase projects?

Many apps need only a single Firebase project and the default set up described in the Get Started guides. Examples of when it can be useful to use multiple Firebase projects include: Setting up your development environment to use different Firebase projects based on build type or target.

Can Firebase have multiple databases?

Create multiple Realtime Database instances If you're on the Blaze pricing plan, you can create multiple database instances in the same Firebase project. In 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.

How many Firebase projects can I have?

Firebase restricts the total number of Firebase Apps within a Firebase project to 30. After this number, performance starts to degrade (especially for Google Analytics) and eventually, at a higher number of apps, some product functionality stops working.

Can two apps use same Firebase?

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


2 Answers

You can only be authenticated once in a single page that uses Firebase authentication.

From this page in the Firebase documentation:

All references to a Firebase share the same authentication status. So if you call new Firebase() twice and call auth() on one of them, they will both be authenticated.

It could of course be that the nest-api authentication works differently, but I doubt it.

Update

You provided a quote from another page in the Firebase documentation:

It is not possible to authenticate with multiple credentials to the same Firebase simultaneously, even if we call .auth on different Firebase references. Authentication state is global and applies to all references to the Firebase. However, it is possible to create references to two or more different Firebases and authenticate to those independently.

But in your code both connections are to the same Firebase: https://developer-api.nest.com. One Firebase => one authentication state.

like image 60
Frank van Puffelen Avatar answered Sep 22 '22 11:09

Frank van Puffelen


You can make multiple instances using new Firebase.Context() as second parameter!

    var token1 = "c.AdASdsAsds...";
    var token2 = "c.dkasdEddss...";

    var v1 = new Firebase('https://developer-api.nest.com', new Firebase.Context());

    v1.auth(token1,function(err){ 
        console.log("Connected 1 "+err);
    },function(err){ console.log("Cancel 1: "+err); });

    var v2 = new Firebase('https://developer-api.nest.com', new Firebase.Context());

    v2.auth(token2,function(err){ 
        console.log("Connected 2 "+err);
    },function(err){ console.log("Cancel 2 "+err); });
like image 37
Kashif Chandio Avatar answered Sep 21 '22 11:09

Kashif Chandio