### Setup and Configuration Source: https://github.com/firebase/quickstart-nodejs/blob/master/machine-learning/README.md Instructions for setting up the Node.js environment, installing dependencies, and configuring the script with service account keys and storage bucket names. ```bash git clone https://github.com/firebase/quickstart-nodejs.git cd quickstart-nodejs/machine-learning npm install chmod u+x manage-ml.js ``` ```javascript const SERVICE_ACCOUNT_KEY = '/path/to/your/service_account_key.json'; const STORAGE_BUCKET = 'your-storage-bucket'; ``` -------------------------------- ### Install Dependencies and Clone Repository Source: https://github.com/firebase/quickstart-nodejs/blob/master/auth-sessions/README.md Installs project dependencies and clones the Node.js quickstart repository from GitHub. ```bash git clone https://github.com/firebase/quickstart-nodejs cd quickstart-nodejs/auth-sessions npm install ``` -------------------------------- ### Install googleapis Source: https://github.com/firebase/quickstart-nodejs/blob/master/messaging/README.md Installs the necessary googleapis library for interacting with Firebase Cloud Messaging. ```bash npm install googleapis ``` -------------------------------- ### Install Dependencies and Run Node.js App Source: https://github.com/firebase/quickstart-nodejs/blob/master/database/README.md Installs project dependencies using npm and runs the Node.js application locally. Ensure you have configured your Firebase project and email transport in index.js. ```bash npm install node index.js ``` -------------------------------- ### Question Reporting Template Source: https://github.com/firebase/quickstart-nodejs/blob/master/CONTRIBUTING.md A template for users to ask questions about the Firebase Node.JS Quickstarts, guiding them to provide context about what they are trying to achieve and where they have looked for information. ```markdown What are you trying to do or find out more about? Where have you looked? Where did you expect to find this information? ``` -------------------------------- ### Firebase Realtime Database (Node.js) Source: https://github.com/firebase/quickstart-nodejs/blob/master/README.md Demonstrates connecting to and using the Firebase Realtime Database with Node.js. ```nodejs const admin = require('firebase-admin'); // Initialize Firebase Admin SDK (replace with your service account key) admin.initializeApp({ credential: admin.credential.cert({ projectId: 'YOUR_PROJECT_ID', clientEmail: 'YOUR_CLIENT_EMAIL', privateKey: 'YOUR_PRIVATE_KEY' }), databaseURL: 'https://YOUR_PROJECT_ID.firebaseio.com' }); const db = admin.database(); // Example of writing data to the database async function writeData(path, data) { try { await db.ref(path).set(data); console.log('Data written successfully to', path); } catch (error) { console.error('Error writing data:', error); throw error; } } // Example of reading data from the database async function readData(path) { try { const snapshot = await db.ref(path).once('value'); const data = snapshot.val(); console.log('Data read from', path, ':', data); return data; } catch (error) { console.error('Error reading data:', error); throw error; } } // Usage example: // writeData('/users/user1', { name: 'Alice', email: 'alice@example.com' }); // readData('/users/user1'); ``` -------------------------------- ### Run Sample App Source: https://github.com/firebase/quickstart-nodejs/blob/master/auth-sessions/README.md Builds and runs the sample application, launching a local server on port 3000. ```bash npm run demo ``` -------------------------------- ### Firebase Machine Learning Management (Node.js) Source: https://github.com/firebase/quickstart-nodejs/blob/master/README.md Demonstrates how to manage your hosted ML models using the Node.js Admin SDK. ```nodejs const admin = require('firebase-admin'); // Initialize Firebase Admin SDK (replace with your service account key) admin.initializeApp({ credential: admin.credential.cert({ projectId: 'YOUR_PROJECT_ID', clientEmail: 'YOUR_CLIENT_EMAIL', privateKey: 'YOUR_PRIVATE_KEY' }) }); const machineLearning = admin.machineLearning(); // Example of listing ML models async function listMLModels() { try { const models = await machineLearning.listModels(); console.log('ML Models:', models.models); return models.models; } catch (error) { console.error('Error listing ML models:', error); throw error; } } // Example of getting details for a specific ML model async function getMLModel(modelId) { try { const model = await machineLearning.getModel(modelId); console.log('ML Model details:', model); return model; } catch (error) { console.error(`Error getting ML model ${modelId}:`, error); throw error; } } // Usage example: // listMLModels(); // getMLModel('your-model-id'); ``` -------------------------------- ### Checking Out Master Branch Source: https://github.com/firebase/quickstart-nodejs/blob/master/CONTRIBUTING.md Switches the current working branch back to the master branch. ```shell git checkout master -f ``` -------------------------------- ### Building and Testing Locally Source: https://github.com/firebase/quickstart-nodejs/blob/master/CONTRIBUTING.md Builds the project locally to ensure all tests pass before submitting a pull request. ```shell gulp ``` -------------------------------- ### Get Active Remote Config Template Source: https://github.com/firebase/quickstart-nodejs/blob/master/config/README.md Retrieves the currently active Firebase Remote Config template. The output is saved to 'config.json', and the ETag is printed to the console for subsequent updates. ```javascript node index.js get ``` -------------------------------- ### Pushing Branch to GitHub Source: https://github.com/firebase/quickstart-nodejs/blob/master/CONTRIBUTING.md Pushes the local feature branch to the remote GitHub repository. ```shell git push origin my-fix-branch ``` -------------------------------- ### Update Remote Config Template from File Source: https://github.com/firebase/quickstart-nodejs/blob/master/config/README.md Updates the Firebase Remote Config template using a local JSON file. Ensure the ETag in the file matches the latest ETag from a previous 'get' operation, or use the 'force' option with caution. ```javascript node index.js publish ``` -------------------------------- ### Firebase Cloud Messaging (Node.js) Source: https://github.com/firebase/quickstart-nodejs/blob/master/README.md Demonstrates sending FCM notification messages to a topic using the Node.js Admin SDK. ```nodejs const admin = require('firebase-admin'); // Initialize Firebase Admin SDK (replace with your service account key) admin.initializeApp({ credential: admin.credential.cert({ projectId: 'YOUR_PROJECT_ID', clientEmail: 'YOUR_CLIENT_EMAIL', privateKey: 'YOUR_PRIVATE_KEY' }) }); const messaging = admin.messaging(); // Example of sending a message to a topic async function sendMessageToTopic(topic, title, body) { const message = { notification: { title: title, body: body }, topic: topic }; try { const response = await messaging.send(message); console.log('Successfully sent message:', response); return response; } catch (error) { console.error('Error sending message:', error); throw error; } } // Usage example: // sendMessageToTopic('news', 'Breaking News', 'Firebase is awesome!'); ``` -------------------------------- ### Creating a Git Branch Source: https://github.com/firebase/quickstart-nodejs/blob/master/CONTRIBUTING.md Creates a new git branch for making changes, based on the master branch. ```shell git checkout -b my-fix-branch master ``` -------------------------------- ### Firebase Remote Config (Node.js) Source: https://github.com/firebase/quickstart-nodejs/blob/master/README.md Demonstrates retrieving and updating the Firebase Remote Config template using the Node.js Admin SDK. ```nodejs const admin = require('firebase-admin'); // Initialize Firebase Admin SDK (replace with your service account key) admin.initializeApp({ credential: admin.credential.cert({ projectId: 'YOUR_PROJECT_ID', clientEmail: 'YOUR_CLIENT_EMAIL', privateKey: 'YOUR_PRIVATE_KEY' }) }); const remoteConfig = admin.remoteConfig(); // Example of getting the Remote Config template async function getRemoteConfigTemplate() { try { const template = await remoteConfig.getTemplate(); console.log('Remote Config template:', JSON.stringify(template, null, 2)); return template; } catch (error) { console.error('Error getting Remote Config template:', error); throw error; } } // Example of updating the Remote Config template async function updateRemoteConfigTemplate(newTemplate) { try { const updatedTemplate = await remoteConfig.publishTemplate(newTemplate); console.log('Remote Config template updated:', JSON.stringify(updatedTemplate, null, 2)); return updatedTemplate; } catch (error) { console.error('Error updating Remote Config template:', error); throw error; } } // Usage example: // getRemoteConfigTemplate(); // const myNewTemplate = { ... }; // Define your new template structure // updateRemoteConfigTemplate(myNewTemplate); ``` -------------------------------- ### Committing Changes Source: https://github.com/firebase/quickstart-nodejs/blob/master/CONTRIBUTING.md Commits staged changes with an optional flag to automatically stage edited files. ```shell git commit -a ``` -------------------------------- ### Firebase ML Model Management Commands Source: https://github.com/firebase/quickstart-nodejs/blob/master/machine-learning/README.md Demonstrates the command-line interface for managing ML models using the Firebase Admin SDK. Includes listing, creating, updating, and deleting models with various options. ```bash ./manage-ml.js list ./manage-ml.js new yak_detector -f ~/yak.tflite --tags vision,experimental ./manage-ml.js new emu_detector -a projects/12345/locations/us-central1/models/ICN12345 ./manage-ml.js update 8717019 --remove_tags experimental ./manage-ml.js delete 8716959 ``` -------------------------------- ### Deleting a Remote Branch Source: https://github.com/firebase/quickstart-nodejs/blob/master/CONTRIBUTING.md Deletes a feature branch from the remote GitHub repository after it has been merged. ```shell git push origin --delete my-fix-branch ``` -------------------------------- ### Firebase Configuration Source: https://github.com/firebase/quickstart-nodejs/blob/master/auth-sessions/README.md Configuration file for Firebase project settings, including API keys and other essential parameters. This file should be created in the root folder of the application. ```javascript const config = { apiKey: "...", authDomain: "...", databaseURL: "...", storageBucket: "...", messagingSenderId: "..." }; module.exports = config; ``` -------------------------------- ### Firebase Authentication Sessions (Node.js) Source: https://github.com/firebase/quickstart-nodejs/blob/master/README.md Demonstrates how to use Firebase httpOnly session cookies with the Firebase Admin SDK session management API. ```nodejs const admin = require('firebase-admin'); // Initialize Firebase Admin SDK (replace with your service account key) admin.initializeApp({ credential: admin.credential.cert({ projectId: 'YOUR_PROJECT_ID', clientEmail: 'YOUR_CLIENT_EMAIL', privateKey: 'YOUR_PRIVATE_KEY' }) }); // Example of creating a session cookie (replace with actual user ID) async function createSessionCookie(uid) { try { const sessionCookie = await admin.auth().createSessionCookie(uid, { expiresIn: 60 * 60 * 24 * 5 }); // 5 days console.log('Session cookie created:', sessionCookie); return sessionCookie; } catch (error) { console.error('Error creating session cookie:', error); throw error; } } // Example of verifying a session cookie async function verifySessionCookie(sessionCookie) { try { const decodedClaims = await admin.auth().verifySessionCookie(sessionCookie); console.log('Session cookie verified. User ID:', decodedClaims.uid); return decodedClaims; } catch (error) { console.error('Error verifying session cookie:', error); throw error; } } // Usage example: // createSessionCookie('some-user-id').then(cookie => { // verifySessionCookie(cookie); // }); ``` -------------------------------- ### Updating Local Master with Upstream Source: https://github.com/firebase/quickstart-nodejs/blob/master/CONTRIBUTING.md Fetches the latest changes from the upstream repository and merges them into the local master branch. ```shell git pull --ff upstream master ``` -------------------------------- ### Issue Reporting Template Source: https://github.com/firebase/quickstart-nodejs/blob/master/CONTRIBUTING.md A template to help users report issues effectively, including browser/OS details, reproduction steps, expected vs. actual results, and additional information. ```markdown Browser: Browser version: Operating system: Operating system version: What steps will reproduce the problem: 1. 2. 3. What is the expected result? What happens instead of that? Please provide any other information below, and attach a screenshot if possible. ``` -------------------------------- ### Firebase Node.js Realtime Database Configuration Source: https://github.com/firebase/quickstart-nodejs/blob/master/database/README.md Illustrates the configuration placeholders in `index.js` for connecting to Firebase Realtime Database. This includes setting the project ID and the path to the service account credentials file, as well as configuring email transport. ```javascript // Change the and placeholders in index.js // Configure your email transport in index.js ``` -------------------------------- ### Deleting a Local Branch Source: https://github.com/firebase/quickstart-nodejs/blob/master/CONTRIBUTING.md Deletes the local feature branch after it has been merged and the remote branch is removed. ```shell git branch -D my-fix-branch ``` -------------------------------- ### Rebasing and Force Pushing Updates Source: https://github.com/firebase/quickstart-nodejs/blob/master/CONTRIBUTING.md Updates the feature branch with changes from the master branch and force pushes to GitHub to update the pull request. ```shell git rebase master -i git push origin my-fix-branch -f ``` -------------------------------- ### Send Overridden FCM Message Source: https://github.com/firebase/quickstart-nodejs/blob/master/messaging/README.md Sends a notification message with platform-specific customizations for Android (click_action) and iOS (priority, badge count). ```nodejs node index.js override-message ``` -------------------------------- ### Send Common FCM Message Source: https://github.com/firebase/quickstart-nodejs/blob/master/messaging/README.md Sends a basic notification message with a common title and body to all subscribed clients via FCM. ```nodejs node index.js common-message ``` -------------------------------- ### Update Remote Config Template in Memory Source: https://github.com/firebase/quickstart-nodejs/blob/master/config/README.md Retrieves the current Remote Config template, modifies it in memory by setting a condition and a parameter, and then updates the template on the server. ```javascript node index.js update ``` -------------------------------- ### Firebase Remote Config REST API Operations Source: https://github.com/firebase/quickstart-nodejs/blob/master/config/README.md Provides an overview of operations available through the Firebase Remote Config REST API for managing configuration templates. Key operations include retrieving the template, updating it, and managing versions. ```APIDOC Firebase Remote Config REST API: - Get Template: Retrieves the current Remote Config template. Requires the latest ETag for conditional updates. - Update Template: Updates the Remote Config template. Parameters: - etag: The ETag of the template to update. Must match the latest ETag from a 'get' operation. - force: (Optional) If true, overwrites the current template regardless of ETag. Use with caution. Returns: The updated template with a new ETag. - Publish Template: Publishes a new version of the Remote Config template. This operation typically involves sending the modified template content. Best Practices: - ETags: Always use the ETag returned from a 'get' operation when updating to prevent accidental overwrites. - Force Update: Use the 'force' parameter only when you intend to overwrite any existing changes. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.