Can a single emulator support multiple firestore databases for the same project?

1,458 views
Skip to first unread message

Stephen Cagle

unread,
Oct 12, 2020, 6:24:03 PM10/12/20
to Firebase Google Group
So while I am developing I have two firestore emulators spun up on separate ports. 

Emulator 1 - testing firestore server rules:
On localhost:9090 I have the emulator that I use exclusively for testing my server rules. My server rule test are run automatically on file save of firestore.rules . Every test function of my firestore rules clears out the firestore database at localhost:9090 upon completion. So if there are N test of firestore rules than I clear out the database at localhost:9090 N times before getting the test results. Simple enough.

Emulator 2 - persistent database for interactive development:
On localhost:9095 I have the emulator that I use as I am developing a feature. It is usually only cleared (manually) by me between development of a feature. I also run the emulator UI on this one so that I can view/edit the data with the web-ui as I am working.

ASK:
I currently spin up separate emulators in separate windows using `firebase emulators:start`, they each read their local firebase.json file in start up appropriately.  I am wondering if it is possible to use a single emulator with different ports pointing to different instances of the firestore database though? To be clear, same security rules, but isolated databases that can be cleared for different reasons independently.



Minor 1:
Upon starting the second emulator, I usually get this little message

```
emulators: Starting emulators: firestore
⚠  hub: emulator hub unable to start on port 4400, starting on 4401 instead.
i  firestore: Firestore Emulator logging to firestore-debug.log

┌──────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! It is now safe to connect your apps. │
└──────────────────────────────────────────────────────────────┘

┌───────────┬────────────────┐
│ Emulator  │ Host:Port      │
├───────────┼────────────────┤
│ Firestore │ localhost:8095 │
└───────────┴────────────────┘
  Other reserved ports: 4401
```
Does not seem to make a huge difference, not sure what the hub thing is about.



Minor 2:
I think this may be unrelated to running multiple emulators, but sometimes the emulator(s) does not die properly. I have to `kill ` them manually after pressing Ctrl-C. My machine:

```
➜  ~ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.1 LTS
Release: 20.04
Codename: focal
```

Sam Stern

unread,
Oct 13, 2020, 6:06:46 AM10/13/20
to Firebase Google Group
Hi Stephen,

Thanks for the extremely thorough/clear feedback.  Each "emulators:start" command can only start up one copy of each emulator binary, and each binary can only bind to one port.  So there's no way to have a single start command launch Firestore on both 9090 and 9095, as you mentioned.  And that's not something we're planning on building right now.

However each Firestore emulator process will happily serve multiple distinct databases.  If you just change your projectId, databases will be implicitly created as needed and their data/rules can be changed independently.  And the project Ids don't have to be real!  So I'd recommend:
  • Use your real project ID for your interactive development.
  • Use a fake project ID (could literally be "fakeproject") for your security rules unit tests.
That way you can do everything with a single emulator instance.

- Sam

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/4c6f631f-f171-41fe-bfe3-538885e2d468n%40googlegroups.com.

Stephen Cagle

unread,
Oct 14, 2020, 1:03:43 PM10/14/20
to Firebase Google Group
I have done as you suggested this morning and am now running everything with just one Emulator (all on one port). I appreciate that `firebase.clearFirestoreData({projectId: MY_PROJECT_ID})` allows me to clear per database, rather than clearing/reseting the entire emulator. 

Minor question. I was curious on how the database in the emulator UI at http://localhost:4000/firestore is being selected? My guess is that it is always the `[Default]` database? I don't have it as a use case currently (I clear out the database between every test), but how would I use the UI to look at other databases other than the default one? The behavior I am seeing is that the emulator UI is always displaying my "interactive development" database (which is what I want). I am curious how I view other databases within the UI?

Thank you for your help!

Sam Stern

unread,
Oct 14, 2020, 1:34:59 PM10/14/20
to Firebase Google Group
Hi Stephen,

So the Emulator Suite does have a notion of a "default" project.  This can be passed via --project or inferred from .firebaserc.  This is the only project which gets the full setup, including function triggers between services, import/export in Firetore, etc.  So that's the one the UI shows.  We may add support to the UI for showing multiple Firestore databases, but I think for the near future the Emulator Suite will still mostly assume the developer has a single project.

- Sam

will

unread,
Apr 8, 2021, 7:46:48 PM4/8/21
to Firebase Google Group
Hey guys! Thanks for this thread, it was very helpful!

I have a similar setup as the one used by Stephen. I'm running the emulator for interactive development with my Next.js app but I also like to keep my tests running in the background at the same time.

Sam's suggestion of giving a fake projectId does work for unit testing security rules. However, it doesn't work if we want to have some integration tests with Cloud Functions. As far as I can tell, Cloud Functions is using the same projectId as the one I'm using for the interactive development.

Is this correct or am I missing something?

Sam Stern

unread,
Apr 9, 2021, 6:32:37 AM4/9/21
to Firebase Google Group
Hi Will,

That's correct. The emulators suite only emulates a single project ID at the moment when it comes to connections between emulators. So writes to another project ID will be accepted by the Firestore emulator, but no functions will trigger.

We don't have any plans to change this in the near term, in fact we are considering making things even stricter by default since so many developers seem to end up very confused when they have mismatched/multiple project IDs and it feels like someone snipped a wire.

However it should be possible to run more than one entire instance of the emulator suite at once if you want two isolated instances but still need to test functions<>firestore interactions.  You'd just need to separate their ports, which you can do by having a separate firebase.json and passing it in with the --config flag.

- Sam

Will Ceolin

unread,
Apr 9, 2021, 6:45:44 AM4/9/21
to fireba...@googlegroups.com
Oh, I see. I thought running multiple instances wasn't possible. This is awesome. Thanks, Sam!

You received this message because you are subscribed to a topic in the Google Groups "Firebase Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/firebase-talk/gSbBof-GNI0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/CAHafJBq9Bk9OZBrUyjkNFe632%3DwY3VM16hHRzQfEP0tUWk-_cA%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages