11 minute read

Exploring Firebase

Exploring Firebase

What is Firebase?

Originally, Firebase started as a product called Envolve in 2011, which was a real-time chat service. It was co-founded by James Tamplin and Andrew Lee. While developing Envolve, they realized that developers were using its real-time system to sync application data, not just chat messages. This led them to pivot and create Firebase as a real-time database product in 2012. Google acquired Firebase in 2014 and integrated it into its broader ecosystem, expanding it into a full-fledged platform for app development.

As of 2024, Firebase is a platform developed by Google that provides tools for building and managing web and mobile applications. It offers various services like databases, authentication, analytics, cloud storage, and serverless computing, allowing developers to quickly integrate backend functionalities into their apps without needing to manage their own infrastructure.

Key features of Firebase

Firebase offers a wide range of features for building and managing web and mobile applications. Here’s a line-by-line description of its key features:

  1. Firebase Authentication: Provides easy sign-in options with email, password, phone number, Google, Facebook, and more.
  2. Firebase Realtime Database: Stores and syncs data in real-time across all clients connected to the application.
  3. Cloud Firestore: A scalable, flexible NoSQL database for storing, querying, and syncing data across multiple devices in real-time.
  4. Firebase Cloud Storage: Securely stores and serves user-generated content like images, videos, and audio.
  5. Firebase Hosting: A fast and secure static web hosting service for modern web apps with built-in SSL and CDN.
  6. Firebase Cloud Functions: Serverless functions that run backend code in response to events triggered by Firebase features or HTTPS requests.
  7. Firebase Cloud Messaging (FCM): Enables sending notifications and messages to users across platforms like iOS, Android, and the web.
  8. Firebase Analytics: Provides free, unlimited analytics for monitoring user engagement and app usage patterns.
  9. Firebase Crashlytics: Helps track, prioritize, and fix stability issues and crashes in real-time.
  10. Firebase Performance Monitoring: Monitors app performance in real-time, highlighting any issues with latency or crashes.
  11. Firebase Test Lab: Allows testing your apps on physical and virtual devices hosted by Google’s data centers.
  12. Firebase Remote Config: Dynamically updates and configures your app without needing to publish a new version.
  13. Firebase App Distribution: Distributes pre-release versions of your app to testers seamlessly.
  14. Firebase Dynamic Links: Provides deep linking capabilities that work across different platforms, ensuring users are directed to the right content.
  15. Firebase ML Kit: Offers machine learning capabilities such as text recognition, image labeling, and face detection, integrated into your app.

Each of these features helps streamline app development and improve the user experience while offloading backend infrastructure management to Firebase. Whatever application you are building you can focus on the key features of that and integrate that application with Firebase for the above features. You will save a lot of time in developing, testing, maintainting, upgrading, securing these features. Yes, there will integration cost and firebase API call but that you need to incur even for your own developed apps.

What are they Firebase commands?

Here’s a grouped version of the Firebase CLI commands based on their functionality:

Project Setup & Management

  1. firebase init: Initializes a new Firebase project and configures the current directory for Firebase services.
  2. firebase login: Logs you into your Firebase account.
  3. firebase logout: Logs you out of your Firebase account.
  4. firebase use: Switches between different Firebase projects within the current directory.
  5. firebase projects:list: Lists all Firebase projects in your account.

Deployment

  1. firebase deploy: Deploys your Firebase project to Firebase Hosting, Functions, Firestore, or Realtime Database.
  2. firebase deploy –only functions: Deploys only Firebase Functions from your project.
  3. firebase deploy –only hosting: Deploys only Firebase Hosting resources from your project.
  4. firebase deploy –only firestore: Deploys only Firestore rules and indexes from your project.
  5. firebase deploy –only database: Deploys only Realtime Database rules from your project.
  6. firebase deploy –only storage: Deploys only Cloud Storage rules from your project.
  7. firebase deploy –only firestore:indexes: Deploys only Firestore indexes defined in the firestore.indexes.json file.
  8. firebase deploy –only storage:rules: Deploys only Cloud Storage security rules.

Emulators & Local Development

  1. firebase serve: Starts a local development server to preview your Firebase project.
  2. firebase emulators:start: Starts local Firebase emulators for services like Firestore, Realtime Database, and Functions.
  3. firebase emulators:exec: Runs a script with Firebase emulators and stops them after execution.
  4. firebase emulators:export: Exports emulator data to a specified directory.
  5. firebase emulators:import: Imports previously exported emulator data.
  6. firebase functions:shell: Starts an interactive shell for testing Firebase Functions locally.

Hosting

  1. firebase hosting:disable: Disables Firebase Hosting for the current project.
  2. firebase hosting:channel:deploy: Deploys to a preview channel for Firebase Hosting.
  3. firebase hosting:channel:list: Lists all preview channels for Firebase Hosting.
  4. firebase hosting:channel:delete: Deletes a preview channel from Firebase Hosting.
  5. firebase hosting:clone: Clones Firebase Hosting site content from one project to another.

Functions

  1. firebase deploy –only functions: Deploys only Firebase Functions.
  2. firebase functions:shell: Starts an interactive shell for testing Functions locally.
  3. firebase functions:log: Displays logs from Firebase Functions.

Realtime Database

  1. firebase database:get: Fetches data from the Firebase Realtime Database.
  2. firebase database:set: Sets data in the Firebase Realtime Database.
  3. firebase database:push: Pushes new data to the Firebase Realtime Database.
  4. firebase database:remove: Deletes data from the Firebase Realtime Database.

Firestore

  1. firebase firestore:delete: Deletes documents or collections in Firestore.
  2. firebase deploy –only firestore: Deploys only Firestore rules and indexes.
  3. firebase deploy –only firestore:indexes: Deploys only Firestore indexes.

Cloud Storage

  1. firebase deploy –only storage: Deploys only Cloud Storage rules.
  2. firebase deploy –only storage:rules: Deploys only Cloud Storage security rules.

Extensions

  1. firebase ext:install: Installs an official Firebase extension.
  2. firebase ext:uninstall: Uninstalls a previously installed Firebase extension.
  3. firebase ext:update: Updates a Firebase extension to the latest version.

Other Utilities

  1. firebase open: Opens Firebase Console in your browser for the active project.
  2. firebase apps:list: Lists all Firebase apps (iOS, Android, Web) in the current project.

How to integrate Firebase into my project?

To integrate Firebase into your project, you can follow these steps depending on the type of project (web, Android, iOS, etc.). Here’s a general guide, focusing on web and mobile projects:

1. Set Up a Firebase Project

  1. Go to the Firebase Console: Visit Firebase Console.
  2. Create a New Project:
    • Click “Add Project” and follow the steps to create a new Firebase project.
    • Optionally enable Google Analytics if you need it.
  3. Add an App to Your Project: In the Firebase project dashboard, click on the platform you’re using (Web, Android, or iOS) to add the respective app.

2. For Web Integration

Step-by-Step Guide:

  1. Install Firebase in Your Project:
    • If you’re using npm for a JavaScript/TypeScript project, install Firebase with:
      npm install firebase
      
  2. Add Firebase SDK:
    • Alternatively, include the Firebase SDK in your HTML if you’re working with a plain HTML/JS project:
      <script src="https://www.gstatic.com/firebasejs/9.x.x/firebase-app.js"></script>
      <script src="https://www.gstatic.com/firebasejs/9.x.x/firebase-auth.js"></script>
      <script src="https://www.gstatic.com/firebasejs/9.x.x/firebase-firestore.js"></script>
      <!-- Add other Firebase SDKs as needed -->
      
  3. Initialize Firebase:
    • After setting up Firebase, initialize it in your code by copying the configuration snippet provided in the Firebase Console:
      // Import Firebase
      import { initializeApp } from "firebase/app";
      
      // Firebase config (replace with your actual config)
      const firebaseConfig = {
        apiKey: "YOUR_API_KEY",
        authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
        projectId: "YOUR_PROJECT_ID",
        storageBucket: "YOUR_PROJECT_ID.appspot.com",
        messagingSenderId: "YOUR_MESSAGING_SENDER_ID",
        appId: "YOUR_APP_ID"
      };
      
      // Initialize Firebase
      const app = initializeApp(firebaseConfig);
      
  4. Use Firebase Services:
    • Import and use the Firebase services you need (like Firestore, Authentication, etc.). For example:
      import { getFirestore, doc, setDoc } from "firebase/firestore"; 
      const db = getFirestore(app);
      
      // Example of setting data in Firestore
      await setDoc(doc(db, "users", "userID"), { name: "John", age: 25 });
      

3. For Android Integration

Step-by-Step Guide:

  1. Add Firebase SDK:
    • In your Firebase project, select “Add App” and choose Android.
    • Follow the steps to register your Android app (you’ll need your app’s package name).
    • Download the google-services.json file and place it in the app/ directory of your Android project.
  2. Configure Firebase SDK in Android Studio:
    • Open your android/build.gradle (Project-level) and add:
      classpath 'com.google.gms:google-services:4.3.10'
      
    • Open android/app/build.gradle (App-level) and add:
      apply plugin: 'com.google.gms.google-services'
      
  3. Add Dependencies:
    • In android/app/build.gradle, add Firebase dependencies for the services you want to use. For example, for Firebase Authentication and Firestore:
      implementation 'com.google.firebase:firebase-auth:21.0.1'
      implementation 'com.google.firebase:firebase-firestore:24.0.0'
      
  4. Sync the Project: Run “Sync Project with Gradle Files” in Android Studio.

  5. Initialize Firebase:
    • Firebase is automatically initialized in Android, but you can manually do so in your MainActivity.java or MainActivity.kt:
      FirebaseApp.initializeApp(this);
      

4. For iOS Integration

Step-by-Step Guide:

  1. Add Firebase SDK:
    • In your Firebase project, select “Add App” and choose iOS.
    • Follow the steps to register your iOS app (you’ll need your app’s bundle identifier).
    • Download the GoogleService-Info.plist file and add it to your Xcode project.
  2. Install Firebase via CocoaPods:
    • Open the terminal and navigate to your iOS project directory.
    • Create or open a Podfile and add Firebase dependencies. For example:
      platform :ios, '10.0'
      pod 'Firebase/Auth'
      pod 'Firebase/Firestore'
      
    • Run pod install to install Firebase pods.
  3. Initialize Firebase:
    • In your AppDelegate.swift file, initialize Firebase:
      import Firebase
      
      @UIApplicationMain
      class AppDelegate: UIResponder, UIApplicationDelegate {
        func application(_ application: UIApplication, 
        didFinishLaunchingWithOptions launchOptions: [
        UIApplication.LaunchOptionsKey: Any]?) -> Bool {
          FirebaseApp.configure()
          return true
        }
      }
      

5. Enable and Configure Firebase Services

After integrating Firebase, configure the specific services you need:

  1. Firebase Authentication: Set up email/password, social logins, or phone authentication in Firebase Console, and integrate it using Firebase SDK in your app.
  2. Firestore/Realtime Database: Create databases in the Firebase Console, and access them through the SDK with appropriate rules.
  3. Firebase Cloud Messaging (FCM): Set up notifications by configuring FCM in Firebase Console and integrating it with your app.
  4. Cloud Functions: Deploy serverless functions with Firebase Functions CLI.

6. Firebase CLI for Hosting and Functions

  1. Install Firebase CLI:
    npm install -g firebase-tools
    
  2. Login to Firebase:
    firebase login
    
  3. Initialize Firebase in Your Project:
    firebase init
    
    • Choose the services (Hosting, Functions, Firestore, etc.) during initialization.
  4. Deploy Firebase Services:
    firebase deploy
    

How to do Firebase Integration with existing application?

Let’s assume we already have a python application. This is using LLM for summarising inputs given by the user. There are features in this app. We want this app should be integrated with firebase how to do that?

To integrate Firebase with your Python application that uses a large language model (LLM) for summarization, you can use Firebase’s REST APIs and/or Firebase Admin SDK for Python. This integration would allow you to add features like authentication, database management, cloud storage, and other Firebase services. Here’s how you can integrate Firebase into your Python app:

1. Install Firebase Admin SDK

First, install the Firebase Admin SDK for Python, which allows server-side interactions with Firebase.

pip install firebase-admin

2. Set Up Firebase Project

If you haven’t set up a Firebase project yet, follow these steps:

  • Go to the Firebase Console, create a new project, and add the necessary services (Firestore, Authentication, etc.).
  • Generate the serviceAccountKey.json file (Settings → Service Accounts → Generate New Private Key) and save it in your project directory.

3. Initialize Firebase in Python

You need to initialize Firebase in your Python app using the Admin SDK and the service account key.

import firebase_admin
from firebase_admin import credentials, firestore

# Use the service account key to initialize the app
cred = credentials.Certificate('path/to/serviceAccountKey.json')
firebase_admin.initialize_app(cred)

# Initialize Firestore database (if you want to use Firestore)
db = firestore.client()

4. Firebase Authentication Integration

If you want to add user authentication to your Python app (e.g., sign-up, login), you can use Firebase Authentication. You’ll typically interact with Firebase Authentication on the client side (via web or mobile), but you can verify authentication tokens on the server using Python.

Example of verifying an ID token:

from firebase_admin import auth

def verify_token(id_token):
    try:
        decoded_token = auth.verify_id_token(id_token)
        uid = decoded_token['uid']
        print(f'User ID: {uid}')
    except Exception as e:
        print(f'Error verifying token: {e}')

You can pass the user’s ID token from the client (where the user signed in) to the server for verification.

5. Using Firestore (or Realtime Database)

You can use Firestore or Realtime Database to store and retrieve data related to the summarization inputs, user data, or any other features of your application.

Example: Storing Summarization Results in Firestore

def save_summary_to_firestore(user_id, summary_text):
    doc_ref = db.collection('summaries').document(user_id)
    doc_ref.set({
        'summary': summary_text,
        'timestamp': firestore.SERVER_TIMESTAMP
    })

You can retrieve data from Firestore as follows:

def get_summary(user_id):
    doc_ref = db.collection('summaries').document(user_id)
    doc = doc_ref.get()
    if doc.exists:
        print(f"Summary: {doc.to_dict()['summary']}")
    else:
        print('No summary found')

6. Firebase Cloud Storage

If you need to upload files (e.g., user inputs in files, summaries as text files), you can integrate Firebase Storage.

First, add the google-cloud-storage package:

pip install google-cloud-storage

Next, set up Firebase Storage:

from google.cloud import storage

def upload_file(file_path, storage_bucket_name, destination_blob_name):
    # Initialize storage client
    client = storage.Client()
    bucket = client.get_bucket(storage_bucket_name)
    blob = bucket.blob(destination_blob_name)

    # Upload file to Firebase Storage
    blob.upload_from_filename(file_path)
    print(f'File {file_path} uploaded to {destination_blob_name}')

7. Integrating Firebase Cloud Messaging (FCM)

If you want to send notifications to your users about their summarization results or other updates, you can use Firebase Cloud Messaging.

First, install requests if not already installed:

pip install requests

Send notifications via Firebase FCM:

import requests

def send_fcm_notification(device_token, title, body):
    url = "https://fcm.googleapis.com/fcm/send"
    headers = {
        "Authorization": "key=YOUR_SERVER_KEY",  
		# Found in Firebase Console -> Project Settings -> Cloud Messaging
        "Content-Type": "application/json"
    }
    payload = {
        "to": device_token,
        "notification": {
            "title": title,
            "body": body
        }
    }
    response = requests.post(url, json=payload, headers=headers)
    print(f"Notification sent: {response.status_code}")

8. Deploy Firebase Cloud Functions (Optional)

You can also offload certain functions (e.g., heavy computations or event-based triggers) to Firebase Cloud Functions.

  • First, write your Cloud Function logic using JavaScript/TypeScript.
  • Deploy it via Firebase CLI (firebase deploy --only functions).
  • Call this function from your Python app using REST APIs.

9. Hosting (If You Want to Build a Web Interface)

If your Python application has a front-end or dashboard, you can deploy it using Firebase Hosting.

  • Use Firebase CLI to deploy static websites (HTML, CSS, JavaScript):
    firebase deploy --only hosting
    

10. Firebase Analytics (Optional)

If you want to track user interactions, behavior, or performance of your app, you can use Firebase Analytics. This is mainly client-side, but server-side events can be sent as well.


Overall Integration Flow:

  • User Authentication: Secure user login and registration (Firebase Authentication).
  • Data Storage: Store and retrieve user input, summaries, and preferences (Firestore/Realtime Database).
  • Notifications: Notify users of summaries or updates (Firebase Cloud Messaging).
  • Cloud Storage: Upload and store any files (Firebase Storage).
  • Cloud Functions: Offload event-based logic if needed (e.g., sending emails, background processes).

This setup will allow your Python application to integrate with Firebase’s various services, adding real-time capabilities, authentication, cloud storage, and more.

Hashtags:

#FirebaseIntegration #PythonDevelopment #LLM #CloudServices #BackendDevelopment #FirebaseAdminSDK #Firestore #CloudStorage #AppDevelopment #FirebaseAuth #RealTimeData #CloudMessaging #Serverless

Updated: