5 Transferable Skills You Must Have To Be Successful at ANY Side hustle

The 21st century is fast-paced and highly competitive. Not having the right skills to get ahead is like putting on a blindfold and wondering why you can’t hit the target! With the skills covered in…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Getting started with Firebase emulators suite for Android

Do you know a friend/colleague who tests their application directly in the production environment?

Yeah, that’s a bad practice. We should have a local or staging environment where we test our code before we push to the production. That’s where the Firebase emulator comes into the picture. Firebase emulator is a tool that helps us to test various Firebase services in our local environment altogether.

According to the official documentation

Currently, there are 5 services available in the emulator.

Note: However your app will still continue to communicate with production Firebase services when emulators are not available or configured.

We need the Firestore emulator to safely read and write documents in testing. Since all services are connected to the local environment, these trigger the events in each other’s services.

For example, The realtime database writes may trigger the cloud functions in the emulator.

Because of a lack of testing we face the following issues.

No one writes a perfect code in the first attempt. It’s an iterative process. We need a fast feedback loop. Let’s say that if we are writing a cloud function without an emulator, then every time we do a change we need to push to the server which will take around 20–30 seconds which is a waste of time, causes distraction, and hamper the productivity.

While developing we make a lot of assumptions and mistakes which are fixed by debugging. Without a local setup, debugging directly on the server is a nightmare. No, having console.log() in each line of the code is not a good solution either. Having a local environment allows us to debug more effectively using our favorite IDE.

The most FAQ on Firebase is about data modeling for the database. We keep experimenting with the structure of the database and if we don’t have a local environment then it’s become very difficult to define a clear boundary between the test data and the production data.

The chances of messing up the production data are very high, by doing a typo, using the wrong key, or deleting a document by mistake.

There is one workaround for this which I am going to talk about in the next section.

Each individual product emulator in the Emulator Suite responds to SDK and REST API calls just like production Firebase services. So we can use our own testing tools integrating into our CI/CD to write self-contained integration tests that use the Local Emulator Suite as the back-end.

The pricing won’t cause any problem if you are working on a project as a hobby which does not have any real users. But if you plan for the real customers/users then you will reach a point where you have let say, 10K users. Due to the increase in the number of users, the usage of services will grow proportionally.

Realtime DB will have a thousand simultaneous connections. Firestore will have more read/write/deletes. The cloud function will have high invokes which eventually cost you a lot of money.

So we should be aware of any unnecessary reads and downloads from the Firebase especially while testing because we used to push all kinds of junk data to test various scenarios.

I recommend watching this video for firestore pricing.

For Hindi:

Since we have one database per project most developers create multiple projects to handle production and test environments separately.

For example, I will create a Firebase project with the name “My Prod App” adding my app with package com.burhanrashid52.firebase.sampleapp, and then I'll create another project called "My Test App" with package name com.burhanrashid52.firebase.sampleapp.testing.

Here is my short version of that steps with given commands:

8. Run localhost:4000 in the browser and you will see an emulator suite console.

The dashboard displays the status(On/Off) of each service emulator with a port number. We connect our client app to those port numbers. (More on this later).

Our Firebase emulator suite is up and running. Now we need to connect our client to the emulator suite. After that our client will be able to push and pull the data of this emulator and we can observe the changes on the emulator UI page.

Since we run android on the “android emulator” we cannot use localhost:8080 as a host. We need to use 10.0.2.2:8080 as the "android emulator" IP. We can test that by running 10.0.2.2:8080 in the android emulator browser and verify that firebase emulator UI is displayed in the android emulator browser.

Following are code snippets to set the host. The port is different for each service.

For realtime DB we need to add a project-id in the URL. We can find this in the firebase project setting or directly copy from the realtime database tab from the firebase emulator UI.

We used a flag isFirebaseLocal and set at MyApp class. It helps to toggle between local and production database. We can put this flag in the gradle setting as well.

We can inject using Koin.

For release build, I always make this flag as false. For that, I’ve done something like this. So that I don’t accidentally put true for the release build. There are better ways to do this. But for simplicity, I am going with this.

5. Double-check your keys which we are using for firestore and realtime time. A typo can drain all your mental, physical, and spiritual energy.

Firebase emulator suite is new and far from perfect and sometimes it has bugs.

I’ve developed an app using firebase before and believe me this feature saves a lot of time and energy.

Now tell your friend/colleague that we have a better way of testing the code using a firebase emulator instead of doing in production. It’s just beginning.

Looking forward to more features and bugs fixes. Thanks to Firebase Team

Add a comment

Related posts:

1st Assignment

I usually do academic writing. it includes essay writing and paragraph writing. Though I am not pro at writing. My style of writing is simple and plain. I am trying to learn argumentative and…