3
votes

I am trying to read the events that my device is sending. I am using azure npm lib to read the what i think is right.

Ok so first, under my Azure ioT Hub account for that service there is a tab call Messaging. There is something called "Event Hub-compatible name" and "Event Hub-compativle endpoint". Do i have to create a new Event hub with the name of "Event Hub-compatible name" or what? I am a bit confused :D If not what is the connection string and topic and so on?

Here is how the code is now...

var azure = require('azure');

var serviceBusService = azure.createServiceBusService("Endpoint=XXXXXXXXXX.servicebus.windows.net/");
var isWaiting = false;


function waitForMessages(){
    console.log("Checking Queue...");
    isWaiting = true;
    serviceBusService.receiveQueueMessage("messages","events",function (error, receivedMessage){
        console.log(error);
        console.log(receivedMessage);
        isWaiting = false;
    });
}


// Start messages listener
setInterval(function () {
    if(!isWaiting){
        waitForMessages();
    }
}, 200);
4

4 Answers

4
votes

The Event Hub-compatible name does not mean that you have to create a Event Hub with the same name.

IOT Hub provides an endpoint that is backward compatible with Event Hub API. I think the actual implementation is a little more complex, but you can think of IOT Hub as inheriting from or at least an implementation of Event Hubs. Use this Event Hub compatible name with any Event Hub SDKs or code examples as part of the connection string.

For explaining the concepts of Event Hub-compatible name & Event Hub-compatible endpoint, you can refer to the section How to read from Event Hubs-compatible endpoints of the offical doc Azure IoT Hub developer guide. You can use the Azure Service Bus SDK for .NET or the Event Hubs - Event Processor Host to read events from IoT Hub in C#.

Otherwise, there are two Azure IoT SDKs for NodeJS using connection string: Azure IoT Service SDK (API Reference) & Azure IoT Device SDK (API Reference).

The connection string that you can find it at the one policy of the tab Shared Access Policies in the All settings, please see the pic below from the doc Tutorial: Get started with Azure IoT Hub.

enter image description here

According to your needs for reading events from IoT Hub, you can follow these samples to code using Azure IoT SDKs for NodeJS.

  1. Using Azure IoT Service SDK to list the deviceIds registed in your IoT Hub, please see the sample https://github.com/Azure/azure-iot-sdks/blob/master/node/service/samples/registry_sample.js.
  2. Using Azure IoT Device SDK to monitor events from IoT Hub, please see the sample https://github.com/Azure/azure-iot-sdks/blob/master/node/device/samples/remote_monitoring.js.

Hope it helps. Any concern, please feel free to let me know.

2
votes

You can use the Event Hubs SDK for Node.js to see events/messages sent by your device to your IoT Hub:

https://www.npmjs.com/package/azure-event-hubs

The client object of the event hubs SDK can accept an IoT Hub connection string, so you don't need to use the Event Hubs connection string.

If you're just trying to debug your device and want to verify that it's actually sending messages, you can use the tool called iothub-explorer provided with the Azure IoT Hub SDK:

https://github.com/Azure/azure-iot-sdks/tree/master/tools/iothub-explorer

Also a couple of clarifications regarding the previous answer: the Service SDK allows to send messages to devices, and to read the "feedback" message that the device sends, which is used to know if the device accepted or rejected the command message but contains no data. It doesn't help to read data events sent by the device.

1
votes

Connect to the IoT Hub to receiving the data:

var protocol = 'amqps';
var eventHubHost = '{your event hub-compatible namespace}';
var sasName = 'iothubowner';
var sasKey = '{your iot hub key}';
var eventHubName = '{your event hub-compatible name}';
var numPartitions = 2;

Protocol

var protocol = 'amqps';

It is the Event Hub-compatible endpoint: sb://abcdefnamespace.servicebus.windows.net/ but without the sb:// and .service windows.net/

Like that: abcdefnamespace

var eventHubHost = '{your event hub-compatible namespace}';

Its so OK

var sasName = 'iothubowner';

Primary key, like this: 83wSUdsSdl6iFM4huqiLGFPVI27J2AlAkdCCNvQ==

var sasKey = '{your iot hub key}';

Name like this: iothub-ehub-testsss-12922-ds333s var eventHubName = '{your event hub-compatible name}';

Its so OK

var numPartitions = 2;
0
votes

Every Iot Hub instance comes with a built in endpoint that is compatible with Event Hubs. The Event Hub compatible endpoint you see in the Azure portal would be the connection string pointing to the Event Hub instance from where you can read all the messages sent to your Iot Hub instance.

You can use this connection string to the instantiate the EventHubConsumerClient class from the @azure/event-hubs library and read your messages.

In the event of a failover, it is said that this backing built-in endpoint would change. So, you would have to fetch the new connection string and restart the process. Another option is to use the sample code in the azure-iot-samples-node repo to get the Event Hub compatible connection string by passing in the Iot Hub connection string and then use the @azure/event-hubs library to read your messages. See more in Read from the built-in endpoint