Azure IoT Hubs two way communication with Google firestore

Mfanelo Ndlela 0 Reputation points
2025-09-30T05:28:48.91+00:00

How do I integrate Google Firestore Database with Azure IoT Hub?

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
{count} votes

2 answers

Sort by: Most helpful
  1. Alex Burlachenko 18,310 Reputation points Volunteer Moderator
    2025-09-30T07:11:07.53+00:00

    Mfanelo Ndlela hi

    first, you will use an azure function. this is the perfect glue for this kind of integration. create a new function app, and within it, create a function that is triggered by the iot hub. you can use the 'azure event hubs' trigger, because iot hub presents an event hub compatible endpoint for reading device messages.

    when your function is triggered by a new device message from iot hub, the function's code will run. inside this function, you will write the logic to take the incoming data and write it to your google firestore database. you will need to use the firestore client libraries for .net, python, or node.js, depending on which language you write your function in.

    for the other direction, sending commands from firestore to devices, you need a way to trigger an action when data changes in firestore. google cloud functions have a built in trigger for firestore events. so you would create a google cloud function that is triggered when a document is created or updated in a specific firestore collection. this google function would then call the azure iot hub's rest api to send a cloud to device message to your specific device.

    so, your two way communication looks like this.

    device to cloud.

    iot device > azure iot hub > azure function > google firestore.

    cloud to device.

    web app updates firestore > google cloud function > azure iot hub api > iot device.

    this pattern of using serverless functions as a bridge is very common and works for connecting any two web services that do not have a direct plugin.

    just remember to manage your secrets securely. use azure key vault to store your firestore credentials for the azure function, and use google secret manager for your iot hub connection string in the google cloud function.

    good luck with your project

    rgds,

    Alex

    0 comments No comments

  2. Nikhil Jha (Accenture International Limited) 2,220 Reputation points Microsoft External Staff Moderator
    2025-10-01T13:45:16.7+00:00

    Hello Mfanelo Ndlela,

    The previous answers from Alex provided an excellent architectural overview. The serverless "glue" approach using an Azure Function for device-to-cloud (D2C) data and a Google Cloud Function for cloud-to-device (C2D) commands is indeed the recommended pattern for this integration.

    I'd like to expand on that foundation with some more specific implementation details and best practices to help you build a robust and secure solution.

    Path 1: Device Data to Firestore (D2C) - In-Depth

    To get your device telemetry into Firestore, you'll configure a flow within Azure.

    1. Configure IoT Hub Message Routing: Instead of having your Azure Function listen to all messages, a better practice is to use IoT Hub Message Routing. You can create a custom endpoint (pointing to the Event Hub built into your IoT Hub) and a route that filters messages. For example, you could route only messages where a property like target is equal to firestore. This makes your solution more scalable and prevents the function from processing unnecessary data.
    2. Azure Function (Event Hub Trigger): Your Azure Function will be triggered by messages arriving at this Event Hub endpoint. Inside the function, you'll use the Google Firestore SDK (for Java, Python, Node.js, etc.) to deserialize the incoming JSON message from your device and write it to the appropriate Firestore collection.
    3. Authentication (Azure to Google): For your Azure Function to authenticate with Google Cloud, you should create a Google Cloud Service Account. Store the JSON key file for this service account securely in Azure Key Vault and grant your Azure Function's Managed Identity access to that Key Vault secret. This avoids storing credentials directly in your application code.

    Path 2: Firestore Commands to Device (C2D) - In-Depth

    This is the more complex path and requires careful consideration of the type of command you're sending.

    1. Google Cloud Function (Firestore Trigger): This function will trigger on document creation or updates in a specific Firestore collection (e.g., a /commands collection).
    2. Choosing Your C2D Communication Method: Azure IoT Hub offers two primary ways to send commands to a device, and choosing the right one is crucial. The previous answers mentioned sending a "cloud-to-device message," but it's important to be specific:
      • Direct Methods (Synchronous): Use these for immediate, request-response commands that require the device to be online and respond within a specified timeout. Think of it like a direct phone call. Use Case: "Reboot now," "Get current GPS location," or "Turn on the fan immediately."
      • Cloud-to-Device (C2D) Messages (Asynchronous): Use these for commands or configuration updates that don't need an immediate response. The message is stored in a queue on IoT Hub for the device to pick up when it next connects. Think of it like sending a text message or email. Use Case: "Set telemetry interval to 5 minutes," "Update firmware configuration," or "Send a welcome message."
    3. Authentication (Google to Azure): For your Google Cloud Function to authenticate with the Azure IoT Hub REST API, you should create a Microsoft Entra ID (formerly Azure AD) Service Principal. Grant this Service Principal the "IoT Hub Data Contributor" role on your IoT Hub. You can then securely store the Service Principal's client ID and client secret in Google Secret Manager for your function to use when calling the Azure APIs.

    Let us know if you have further questions!

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.