1

I wanted to know the best practice for invoking a callable (or "onCall") Firebase function from another Firebase callable function, where each of the callable functions are endpoints

I've seen workarounds that use a common Node function that both Firebase functions would invoke, but I want to avoid this.

I am specifically invoking another callable function within the first callable function so that I can monitor each function separately.

I am using the onCall http functions rather than the onRequest http functions to avoid testing and writing middleware.

The method I am using right now is as follows:

// first function invoked
export const firstFunction = https.onCall( async (data) => {
    msg = data.message;
    const firebaseConfig = {
       apiKey: "...",
       authDomain: "...",
       projectId: "...",
       storageBucket: "...",
       messagingSenderId: "...",
       appId: "...",
       measurementId: "...",
    };
   
    const app = initializeApp(firebaseConfig);
    const functions = getFunctions(app, "us-central1");
    connectFunctionsEmulator(functions, "localhost", 5001);
    
    const callingFunction = httpsCallable(functions, "sendEmail");
    const result = await callingFunction(msg);
    console.log( result );
}
// function called by the first function
export const sendEmail = https.onCall( async (data) => {
    sgMail.send( data );
    return { sucess: true };
}

Is there a way I can invoke the callable function without having to initialize the app and use the getFunctions() method?

Thanks

Matthew Keller
  • 283
  • 2
  • 8
  • 2
    Since my answer was down-voted... Does this answer your question? [Calling a Cloud Function from another Cloud Function](https://stackoverflow.com/questions/42784000/calling-a-cloud-function-from-another-cloud-function) – Martin Zeitler May 13 '22 at 21:18
  • This post gives the example of invoking an `onRequest()` function from another `onRequest()` function and I am trying to use `onCall()` functions. The top-rated answer on that discussion is the workaround I referenced in my question, but it does not involve calling another callable http Firebase function from the first one. The example shows both functions calling a third function within Node. I am just looking for the most efficient way to call an `onCall()` Firebase function from another `onCall()` Firebase function – Matthew Keller May 13 '22 at 21:57
  • Can you explain **why** you need to do that, i.e. calling a callable CF from a callable CF? There is most probably a more standard approach to implement your functional need. – Renaud Tarnec May 14 '22 at 07:52
  • @RenaudTarnec I am trying to do this so I can monitor either function in Firebase logs as separate functions. – Matthew Keller May 15 '22 at 17:03
  • 1
    @MatthewKeller Ok, but then you don't necessary need a Callable Cloud Function. You could call a [Pub/Sub Cloud Function](https://firebase.google.com/docs/functions/pubsub-events) that you would trigger by sending a new Pub/Sub message to a specific topic. I can write an answer with the code if you think this would fulfill your needs/requirements. – Renaud Tarnec May 15 '22 at 17:11

0 Answers0