Share via

How to Fetch Messages from Azure IoT Hub in Real-time Using NestJS/Node.js?

Ankit Bavne 20 Reputation points
2025-02-12T05:21:02.01+00:00

We have an Azure IoT Hub where IoT devices ( buttons) are connected. We need to build a NestJS (Node.js) application that connects to IoT Hub or Event Hub to receive messages in real-time when a device sends telemetry data.

Our goal is to listen for incoming events (like button presses) and trigger an action (e.g., creating an activity in another system).

Questions:

What is the best way to fetch messages in real-time from Azure IoT Hub using NestJS (Node.js)?
Should we use Azure Event Hubs or IoT Hub’s direct message streaming?
What is the best practice for handling these messages efficiently in NestJS?
Are there any sample implementations available?
Azure IoT Hub
Azure IoT Hub

An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.

0 comments No comments

Answer accepted by question author

Deepanshu katara 18,150 Reputation points MVP Volunteer Moderator
2025-02-12T06:42:33.25+00:00

Hello, Welcome to MS Q&A

To connect a NestJS Node.js application to Azure IoT Hub or Event Hub for real-time message fetching, you can follow these steps:

  1. Install Required Packages: Use npm to install the necessary Azure SDK packages. For IoT Hub:
     npm install azure-iothub --save
     npm install azure-iot-common --save
     


For Event Hub:

     npm install @azure/event-hubs --save
     npm install @azure/identity --save
     

  1. Set Up Connection:
    • For IoT Hub, create a client using the Client class from the azure-iothub package and connect using a connection string.
    • For Event Hub, authenticate using @azure/identity and retrieve the Event Hub namespace from environment variables.
  2. Fetch Messages:
    • For IoT Hub, you can send messages to devices and receive feedback on message delivery.
    • For Event Hub, you can set up a consumer to listen for incoming messages in real-time.
  3. Example Code: Here’s a basic example of how to set up a connection to IoT Hub:
   const Client = require('azure-iothub').Client;
   const Message = require('azure-iot-common').Message;
   
   const connectionString = '{IoT hub device connection string}';
   const serviceClient = Client.fromConnectionString(connectionString, 'Amqp');
   
   serviceClient.open((err) => {
       if (err) {
           console.error('Could not connect: ' + err.message);
       } else {
           console.log('Client connected');
           // Add code to send or receive messages
       }
   });
   


For more detailed guidance, you can refer to the following resources:

Please check and let us know if you have any questions,

Kindly accept answer if it helps

Thanks
Deepanshu

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

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.