[GCP] Everything you need to know to setup Firebase and GCP emulators — Part 2

Skrew Everything
From The Scratch
Published in
7 min readOct 11, 2020

Generally speaking, cloud helps us to optimize computing costs based on our demands and takes away unnecessary overhead of purchasing, maintaining and forecasting hardware needs. But cloud makes it difficult for us to test our apps locally. Good thing is, Google provides a bunch of emulators to help us test our apps locally without incurring cloud costs.

But if you are pulling an all-nighter and not careful, your GCP bill will be on 🔥🔥🔥

So, to avoid a heart attack at the end of the month, you need to devote your maximum attention while setting up the emulators. BTW, setting up the emulators is not hard but doing it right is crucial.

Emulators provided by the Google Cloud Platform are —

  1. Firebase Functions
  2. Firebase Hosting
  3. Firebase Firestore
  4. Firebase Realtime Database
  5. Google Cloud Spanner
  6. Google Cloud BigTable
  7. Google Cloud PubSub
  8. Google Cloud DataStore

Setting up the above emulators require zero to minimum code change depending on from where(client or server) your code is using GCP.

Google Cloud Platform

To see the list of emulators and other components provided by GCP, use this command —

gcloud components list

Note: Firebase and GCP offers same emulators for pubsub and firestore. You can install it using either one of them.

Note: Even though GCP provides an emulator for Spanner but it is a docker image. Spanner emulator native binary is only available for Linux. The above image is from macOS. That is the reason there is no Spanner emulator in the list. When you start the Spanner emulator, gcloud automatically downloads docker image if there is none present.

From Google Spanner emulator documentation:

Use the cloud spanner emulator docker image even if the platform has a native binary available in the sdk. Currently we only provide a native binary for Linux. For other systems, you must install docker for your platform before starting the emulator.

To install all the emulators —

gcloud components install pubsub-emulator cloud-datastore-emulator cloud-firestore-emulator bigtable

To start the GCP emulators —

gcloud beta emulators [emulator_name] start [flags]

Unlike Firebase, GCP doesn’t provide a nice json file to configure ports. To give your desired port, you need to pass it as command line argument while starting the emulator. If you are feeling lazy, gcloud gives default port values to each emulator.

Pubsub:

Note: If you have started pubsub using firebase tools, then no need to do it using gcloud

gcloud beta emulators pubsub start

Firestore:

Note: If you have started firestore using firebase tools, then no need to do it using gcloud

gcloud beta emulators firestore start

Bigtable:

gcloud beta emulators bigtable start

Spanner:

gcloud beta emulators spanner start
Need Docker to run Spanner emulator

If you are using other than Linux, then you have to install and start Docker before starting the Spanner Emulator.

If you don’t have Spanner Docker image, then a new one is downloaded and started.

If you already have it, then you will see something like this —

DataStore:

gcloud beta emulators datastore start

Sometimes you might encounter this error —

Don’t worry though. It requires a Project to run the datastore. You have 2 options here.

  1. Export a random string to export CLOUDSDK_CORE_PROJECT='my-project-id' . This is not advisable because as this may make datastore not work with other emulators without additional setup.
  2. You need to login and choose your project. Don’t worry, this will not make your code to hit production servers. You can do it with confidence.

After doing one of the above —

Now that we have installed all the required emulators in our working machine and everything is running, next we see how to use those emulators instead of our production GCP servers.

Using Emulators

The above visualisation should be self explanatory on how the emulator works.

GCP

Pubsub, Spanner, Datastore and Bigtable can only be accessed from your GCP or other servers but not from client side.

Unlike Firebase, you don’t have a json file pointing to the local emulators. Just like how we gave the port number through command line while starting the emulators, we need to export that same port numbers to point our services to our emulators.

Pubsub:

Executing this command shows the environment variables you have to export in order to point your services to local emulators.

gcloud beta emulators pubsub env-init

Execute the command returned by the command.

From Google documentation:

On the machine that runs your application, set the PUBSUB_EMULATOR_HOST environment variable and value as directed by the output of the env-init command. You can optionally set the PUBSUB_PROJECT_ID environment variable for the project you want to use for the emulator, but you only need to set PUBSUB_EMULATOR_HOST for your application to connect to the emulator.

Bigtable:

gcloud beta emulators bigtable env-init

Datastore:

gcloud beta emulators datastore env-init

Spanner:

gcloud beta emulators spanner env-init

Firestore:

export FIRESTORE_EMULATOR_HOST=localhost:$PORT

That’s it

https://giphy.com/gifs/b5Cj0cAsg3buE

You must be feeling tired going through all those commands, codes and links.

So, go ahead and bookmark it and don’t be selfish! Share it with fellow developers to help them out

Want to contact me? Twitter.com/@SkrewEverything.

Found any mistakes or got anything to say? Comment down below.

Liked it? 👏👏👏 it ( come on! If I can spend this much time to write then you can spend few seconds to clap it 50 times 🤷) and Share it.

--

--

Skrew Everything
From The Scratch

A wannabe artist 👨‍🎨, but can’t draw 😫. A wannabe athlete 🏃‍♂️,but can’t run 🥵.Found my peace with coding 👨‍💻 and writing ✍️. Twitter.com/SkrewEverything