8

I'm building an app that has an arbitrary amount of 'micro-communities' on it. Currently a user can only belong to one, so when they initially sign up, they enter a secret code which associates them with the relevant community.

Is it wise to separate these 'micro-communities' (which are owned by future clients playing for a space on the app) into separate Firebase projects or keep all the data together?

Daniel van der Merwe
  • 1,570
  • 2
  • 10
  • 20

3 Answers3

6

Firebase now supports accessing multiple projects in one app. No need to go the micro-community route. Here is the tutorial on how to do it: https://firebase.google.com/docs/configure/

I'm currently using it in my project in Android, so if you are having trouble setting it up and are implementing it in Android, feel free to drop a comment!

EDIT

When you configure Firebase with your app you are defaulted to one database account. If you would like to use a second one (or more), continue reading.

In your activity or fragment, you must initialize a FirebaseOptions instance with all your account info, initialize a FirebaseApp with a tag name that represents the second account, then get the instance of your FirebaseApp, and lastly, get a DatabaseReference instance of your second account so you may use it as you like. Here is the block of code I use: (NOTE: the information you enter as the parameters for you FirebaseOptions instance is associated with your SECOND database account):

private void initSecondFirebaseAcct()
{
    FirebaseOptions options = new FirebaseOptions.Builder()
            .setApplicationId("<your application id>")
            .setApiKey("<your api key>")
            .setDatabaseUrl("<your DB url that ends in 'firebaseio.com/' ")
            .build();

    try
    {
        FirebaseApp.initializeApp(this, options, "<database tag>");
    }
    catch (Exception e)
    {
        Log.d("Firebase error", "App already exists");
    }

    mMySecondApp = FirebaseApp.getInstance("<database tag>");
    mSecondDBRef = FirebaseDatabase.getInstance(mMySecondApp).getReference();
}

Hope that helps!

David Velasquez
  • 2,346
  • 1
  • 26
  • 46
  • 1
    Is it possible to have shared user authentication among the projects? – Ayyappa Jun 24 '17 at 11:36
  • @David, I can't seem to wrap my head on how to configure multiple projects within the same IOS app. Could you please give me a hint or answer? https://stackoverflow.com/questions/44866220 – bibscy Jul 02 '17 at 13:18
  • @bibscy I edited my answer that may help you. But I have only done this in Android not iOS so it may not be the same. Hope you figure it out man! – David Velasquez Jul 03 '17 at 01:47
  • @DavidVelasquez I am hitting my head against the wall with one trivial issue where I try to perform a multi-path update on Firebase Database using the REST API. Could you please help. Thank you. https://stackoverflow.com/questions/46100421 – bibscy Sep 08 '17 at 13:24
  • Both projects use same settings on creation (package name, sha-1, etc...) ? – RonTLV Nov 29 '18 at 19:03
  • what is from where i can find this? – Muhammad Arslan Aug 25 '21 at 13:35
4

I recommend you to keep all the communities together. First, because I'm not really sure that you can use multiple Firebase Projects in the same app.

I suggest you to store this "communities" as items in your database with all their current info. For example:

{
    "Communities":{
            "CommunityKey1":{
                "communityName":
                "createdDate":
                ....
            }
            "CommunityKey2":{
                "communityName":
                "createdDate":
                ....
            }
            "CommunityKey3":{
                "communityName":
                "createdDate":
                ....
            }
            ....
        }
}

And when you add your users to your database, just add them an associated field "currentCommunity" or something like that where you store the associated communityID for that user. Then, when you want to filter the users for one community just use:

FirebaseDatabase.getInstance().getReference().child("users").child("currentCommunity");

You can filter else more using .equalTo("CommunityID") at the end of that query. I'm not an expert in Firebase, but I'm doing an app with a similar kind of user filtering(using groups instead of communities and they can belong to more than one), and the best way to do this was using this model.

halfer
  • 19,824
  • 17
  • 99
  • 186
Francisco Durdin Garcia
  • 12,540
  • 9
  • 53
  • 95
2

I've been contemplating and attempting the solve the same thing. So far I find the best way to structure something like your micro-communities with the possible 'pay' aspect is to have the log-in/user-profile/membership association in one project. Then its membership section contains the other firebase project's connectivity information. Then each community is a separate project, all having their own usage analytics, space, bandwidth usage, etc... Yes it is possible to access multiple firebase projects in your app. https://firebase.google.com/docs/configure/

So the next question becomes, can I dynamically/cli clone a firebase project and seed initial its data when someone creates a new 'community'. https://firebase.google.com/docs/cli/

Good Luck :)