Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using locally emulated https.onCall Firebase functions in locally hosted Firebase application

I am writing a Firebase application using the node SDK and vanilla JavaScript on the client side. I am using Firebase Cloud Functions to implement a server that receives requests for my page routes and returns rendered HTML with the https.onRequest method. I am also using Cloud Functions to handle client-server interaction with the https.onCall method.

I develop locally using the firebase serve command. When developing locally, my client seems ignores my local onCall functions, instead calling the route for the deployed onCall functions. I am forced to deploy my onCall functions in order to see changes locally. If I do not deploy each change, my local app will not show any changes to onCall functions. This happens whether I run firebase serve or firebase serve --only=hosting,functions.

When I run my app locally with firebase serve, the pages are generally hosted at localhost:5000. The functions are hosted at localhost:5001. If I call a cloud function on one of these locally hosted pages, like firebase.functions().httpsCallable('functionName') and check the Network panel in my developer tools, I can see the Request URL is https://us-central1-<app-name>.cloudfunctions.net/<functionName>, instead of localhost:5001/<app-name>/us-central1/<functionName>.

This is frustrating me because it means I have to deploy my functions to test each change, rather than testing my local functions through my locally hosted web application.

Have I configured something incorrectly? How can I get my locally hosted app to use my locally emulated onCall cloud functions?

I am not making a single page application or using any view frameworks.

like image 547
David Y. Stephenson Avatar asked Jun 21 '18 18:06

David Y. Stephenson


People also ask

Can you run Firebase locally?

The Firebase Local Emulator Suite is a set of advanced tools for developers looking to build and test apps locally using Cloud Firestore, Realtime Database, Cloud Storage for Firebase, Authentication, Firebase Hosting, Cloud Functions (beta), Pub/Sub (beta), and Firebase Extensions (beta).

What is the difference between onCall http callable and onRequest HTTP request functions?

onRequest creates a standard API endpoint, and you'll use whatever methods your client-side code normally uses to make. HTTP requests to interact with them. onCall creates a callable. Once you get used to them, onCall is less effort to write, but you don't have all the flexibility you might be used to.


1 Answers

It seems like Firebase has yet to implement a solution to point to the local server, so I came up with a little hack. Include the below snippet where you initialize your Firebase project. Hope it helps!

if (process.env.NODE_ENV === 'development') {
  firebase.functions()._url = function (name) {
    return `${process.env.API_URL}/${name}`
  }
}

EDIT: Like goker.cebeci commented below, the correct way to connect to your local emulator is to use functions.useFunctionsEmulator('http://localhost:5001') (change http://localhost:5001 to whatever address your local emulator is running on)

The updated code looks like this:

if (process.env.NODE_ENV === 'development') {
  const functions = firebase.functions()
  functions.useFunctionsEmulator('http://localhost:5001')
}
like image 170
Henrik Wassdahl Avatar answered Sep 20 '22 17:09

Henrik Wassdahl