Firebase Node.js Quickstarts

repository·master·Indexed 21 days ago

https://github.com/firebase/quickstart-nodejs

A collection of quickstart samples demonstrating how to use Firebase products via the Firebase Admin SDK in a Node.js environment. Includes implementations for session management with httpOnly cookies, Remote Config template management, Realtime Database synchronization and notifications, Firebase-hosted ML model management, and sending Cloud Messaging (FCM) notifications.

Tokens
10.5K
Snippets
35
Records
41
Agent score
75%

What's inside firebase-quickstart-nodejs

  1. Overview of the Realtime Database Node.js Quickstart

    master

    This quickstart demonstrates how to use the Firebase Realtime Database in a Node.js environment using a social blogging application model. The server performs the following automated tasks:

    • Star Count Synchronization: Automatically updates the star counts for all posts in the database.
    • Real-time Notifications: Sends email notifications whenever a post receives a new star.
    • Scheduled Reporting: Sends weekly emails listing the top posts.

    This implementation is designed to interoperate with Web, iOS, and Android database quickstarts.

  2. Overview of Firebase Node.js Quickstarts

    master
    This repository provides a collection of quickstart samples designed to demonstrate how to interact with various Firebase products using the Firebase Admin SDK in a Node.js environment. These samples serve as practical implementation guides for server-side Firebase integration.
  3. Use ETags for safe Remote Config updates

    master

    When retrieving a Remote Config template, the server returns an etag. This is a unique identifier for the current version of the template on the server.

    Why use ETags? To prevent accidental overwrites (where two users try to update the template simultaneously), you must include the latest etag in your update request. This ensures your update is based on the most recent version.

    How to force an update: If you need to completely overwrite the server's template regardless of the current version, you can set the force parameter of the publishTemplate method to true.

    WARNING

    Use force: true with caution; this operation cannot be undone and may overwrite changes made by others.

  4. How FCM payloads work: Common vs Platform-specific

    master

    When sending messages via the Firebase Cloud Messaging (FCM) REST API, you can structure your payload in two ways:

    Common Payloads

    Use the common notification object to define fields like title and body. These are automatically translated by FCM into the appropriate platform-equivalent payloads for all receiving devices. This is ideal for sending the same message to multiple platforms.

    Platform Customizations

    To tailor messages for specific operating systems, use platform-specific objects (android or apns) to add or override settings.

    Example Use Case: If you want to send a standard notification to all platforms but want to include extra data payloads specifically for Android clients, you would use the android object to define that data payload. This ensures the extra data is appended only when the message is sent to Android devices.

  5. Setup the Firebase Node.js Realtime Database Quickstart

    master

    To run the Realtime Database quickstart, follow these steps to configure your environment and service account:

    1. Firebase Project: Create a project in the Firebase Console.
    2. Service Account: Create a service account (refer to Adding Firebase to your Server) and place the JSON credentials file in the project directory.
    3. Configuration: Open index.js and update the following placeholders:
      • <PROJECT_ID>: Your Firebase Project ID.
      • <PATH_TO_SERVICE_ACCOUNT_CREDENTIAL_FILE>: The file path to your service account JSON.
      • Email Transport: Configure your email transport settings within index.js to enable notifications.
    4. Installation: Run npm install to install dependencies.
    5. Execution: Run node index.js to start the server.

    To see the app in action, you must also run a client-side quickstart (Web, iOS, or Android) and use it to publish posts. The Node.js server will then automatically update star counts and send email notifications.

    npm install
    node index.js
  6. Retrieve and Update Firebase Remote Config Templates

    master

    This quickstart provides a CLI interface via index.js to manage your Remote Config templates. Navigate to the config directory to run these commands.

    Get the active template

    Retrieve the current template from the server. This command saves the template to a local file named config.json and prints the current ETag to the console.

    node index.js get

    Update the template via a file

    To update the template using a local JSON file (like the config.json generated by the get command):

    1. Modify the values in your JSON file. If the template is empty, ensure you include conditions and parameters.
    2. Run the publish command:
    node index.js publish

    Update the template in memory

    Retrieve the current template and apply updates programmatically in memory without using a local file. This specific command sets a condition (android_en) and a corresponding parameter.

    node index.js update
    # Get template
    node index.js get
    
    # Publish template from file
    node index.js publish
    
    # Update template in memory
    node index.js update
  7. Setup Firebase Cloud Messaging Node.js Quickstart

    master

    To use this quickstart to send notification messages to a topic via the FCM REST API, follow these steps:

    1. Service Account: Create a service account in the Firebase Console, download the JSON key file, and place it in the messaging directory renamed to service-account.json. Ensure index.js points to this file.
    2. Project ID: Open index.js and update the PROJECT_ID variable with your specific Firebase project ID.
    3. Dependencies: Install the required googleapis package.
    npm install googleapis
  8. Manage Firebase-hosted ML models with manage-ml.js

    master

    The manage-ml.js script provides a CLI interface to manage your Firebase ML models.

    Available Commands and Usage

    • List models: View existing models, their IDs, and tags.

      ./manage-ml.js list
    • Create a new model: Upload a new .tflite model file and optionally assign tags.

      ./manage-ml.js new <model_name> -f <path_to_tflite_file> --tags <tag1>,<tag2>

      Example: ./manage-ml.js new yak_detector -f ~/yak.tflite --tags vision,experimental

    • Create a model from an existing resource: Upload a model using a specific resource path.

      ./manage-ml.js new <model_name> -a <resource_path>

      Example: ./manage-ml.js new emu_detector -a projects/12345/locations/us-central1/models/ICN12345

    • Update a model: Modify an existing model (e.g., removing tags).

      ./manage-ml.js update <model_id> --remove_tags <tag_to_remove>

      Example: ./manage-ml.js update 8717019 --remove_tags experimental

    • Delete a model: Remove a model by its ID.

      ./manage-ml.js delete <model_id>
    # Example session workflow
    ./manage-ml.js list
    ./manage-ml.js new yak_detector -f ~/yak.tflite --tags vision,experimental
    ./manage-ml.js update 8717019 --remove_tags experimental
    ./manage-ml.js delete 8716959
  9. Explore Firebase Product Quickstarts

    master

    The repository contains specific implementation guides for the following Firebase products:

    • Firebase Session Management: Using httpOnly session cookies with the Firebase Admin SDK session management API (auth-sessions).
    • Firebase Remote Config: Retrieving and updating the Remote Config template via the REST API (config).
    • Firebase Realtime Database: Connecting to and using the Realtime Database through a Node.js social blogging app example (database).
    • Firebase Cloud Messaging (FCM): Sending notification messages to a topic using the Node.js Admin SDK (messaging).
    • Firebase Machine Learning: Managing hosted ML models using the Firebase Admin SDK (machine-learning).
  10. Configure the Session Management sample app

    master

    The application requires two configuration files in the ./auth-sessions directory:

    1. Web Configuration (config.js)

    Create a config.js file containing your Firebase Web SDK configuration. You can obtain this by clicking the "Web setup" button in the Firebase Console Authentication page.

    const config = {
      apiKey: "...",
      authDomain: "...",
      databaseURL: "...",
      storageBucket: "...",
      messagingSenderId: "..."
    };
    module.exports = config;

    2. Admin SDK Credentials (serviceAccountKeys.json)

    Since the app uses the Firebase Admin SDK, you must provide a service account private key. Save your generated private key as serviceAccountKeys.json in the ./auth-sessions folder. Warning: Never expose this file publicly.

    Authentication Setup

    In the Firebase Console, navigate to Authentication > SIGN-IN METHOD and enable:

    • Google
    • Email/Password

    Ensure your authorized domain (e.g., localhost) is whitelisted in the Firebase Console.

  11. Set up the Firebase Admin Node.js Session Management Quickstart

    master

    To build and run this sample application, which demonstrates using Firebase httpOnly session cookies with the Firebase Admin SDK session management API, follow these steps:

    1. Install Prerequisites: Ensure you have Node.js (>= 6.0.0) and npm installed.
    2. Clone and Install: Download the source and install dependencies.
    3. Configure Firebase: Set up a Firebase project, enable authentication providers, and provide configuration files.
    4. Run the Demo: Launch the local server.
    git clone https://github.com/firebase/quickstart-nodejs
    cd quickstart-nodejs/auth-sessions
    npm install