1

I have been using firebase for different kind of projects with firestore. But, I have not used real-time database due to low concurrent connection and some factors. Firestore also have some limitation such as 10000 per second per database. When I compare firestore with realtime database, It is possible to manage high traffic with many databases using real-time database. I red different articles and stack overflow questions and they had mentioned use cloud task and cloud scheduler to mange write failures in firestore. However, My questions are,

Can I use cloud functions to create real-time databases?

How many real-time databases can be created per project? (I am going to create a database per user, If there 1 Million Users = 1 Million real-time databases)

6
  • Does this answer your question? Is it okay to create many instances of firebase realtime database (>20) in one project From the answer: "There is no documented limit to the number of databases (often referred to as shards) you can have within a project. Each database essentially functions as a completely separate instance, so there isn't any reason to have a limit at all." Commented Jul 19, 2021 at 11:08
  • That question do not mention about creating real-time databases via cloud function Commented Jul 19, 2021 at 11:11
  • I edited the title to clarify the question. Commented Jul 19, 2021 at 12:32
  • If my answer was helpful, you can accept & upvote it else feel free to ask further questions. Commented Jul 19, 2021 at 12:33
  • For your last questions, see stackoverflow.com/a/55314601/209103 Commented Jul 19, 2021 at 13:17

1 Answer 1

2

You can use the Firebase Realtime Database Management API to create new instances of Realtime Database from a Cloud function. You need an access token with these scopes:

[
  'https://www.googleapis.com/auth/cloud-platform',
  'https://www.googleapis.com/auth/firebase'
]

Now the easiest way to do this is use googleapis node module in you cloud function along with your service account.

const { google } = require('googleapis');

exports.createDbInstance = functions.https.onCall((data, context) => {
  const auth = new google.auth.GoogleAuth({
      keyFile: __dirname + '/serviceAccountKey.json',
      scopes: ['https://www.googleapis.com/auth/cloud-platform']
  });

  const accessToken = await auth.getAccessToken()
  const response = await google.firebasedatabase("v1beta")
          .projects
          .locations
          .instances
          .create({ 
            databaseId: "new-db-id", 
            parent: "projects/[PROJECT-ID]/locations/[LOCATION]",
            access_token: accessToken 
          })
  console.log(response)
  return {data: "New Database Created"}
});

You can download a Service Account JSON file from here: https://console.firebase.google.com/u/0/project/_/settings/serviceaccounts/adminsdk

This shouldn't ideally be a callable function with no authentication. Make sure you implement your own logic so only authorized people can call this function.

[PROJECT-ID] is the ID of project in which you want to create the database.

[LOCATION] is the location you want to create the database in. Here is a list of available locations.

Sign up to request clarification or add additional context in comments.

8 Comments

@Dharmaraja, Let me try and I will let you know, Thank you.
@IsuruBandara sure. You can just create a new node project and paste the code and run it as a short cut ;)
@Dharmaraja Can we update the real-time database rules along with this function?
@IsuruBandara Yes, you can update security rules using the REST API
Thank you mate. One last question. Do you think real-time database per user will be practical? Imaging If we have 1 million users mean 1 million databases, and each database can be have 200k Simultaneous connections.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.