Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Latest commit

 

History

History
71 lines (49 loc) · 3.26 KB

FUNCTIONS.md

File metadata and controls

71 lines (49 loc) · 3.26 KB

Cloud Functions

Added in plugin version 7.1.0, by breningham

Cloud Functions?

Cloud Functions for Firebase lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your code is stored in Google's cloud and runs in a managed environment. There's no need to manage and scale your own servers.

Learn more here..

Note that you don't need to enable this feature in the plugin unless you want to call those Cloud Functions from your app.

Enabling Cloud Functions

To add this feature to your project, either:

  • Remove firebase.nativescript.json from the root of the project and run npm i, or
  • Edit that file and add "functions": true.

In both cases, remove the /platforms folder afterwards so the required native library will be added upon the next build.

API

You can use either the Web API syntax (easy for interoperability with a web version of your app), or our custom native syntax. Use whichever syntax you like most - the underlying implementation is the same.

Both API supports Cloud Functions locations/regions as described here. To set a function's region, pass the region string as the second parameter of httpsCallable. If the region parameter is not supplied, the function will use Cloud Function's default region, which as of this doc is written is us-central1.

httpsCallable

This example uses the Cloud Function as implemented here.

Native API
import * as firebase from "nativescript-plugin-firebase";

const fn = firebase.functions.httpsCallable("helloName");

// example of specifying the function's region
// defaults to `us-central1` if not specified
// const fn = firebase.functions.httpsCallable("helloName", "us-central1");

fn("Nativescript-Plugin-Firebase!")
    .then((dataCue: any) => console.log("Callable Function Result: " + dataCue.message))
    .catch((errorMessage: string) => console.log("Callable Function Error: " + errorMessage));
Web API
const firebaseWebApi = require("nativescript-plugin-firebase/app"); // mind the /app!

const fn = firebaseWebApi.functions().httpsCallable("helloName");

// example of specifying the function's region
// defaults to `us-central1` if not specified
// const fn = firebaseWebApi.functions().httpsCallable("helloName", "us-central1");

fn("Nativescript-Plugin-Firebase!")
    .then((dataCue: any) => console.log("Callable Function Result: " + dataCue.message))
    .catch((errorMessage: string) => console.log("Callable Function Error: " + errorMessage));

useFunctionsEmulator

Changes this instance to point to a Cloud Functions emulator running locally. See the Firebase docs.