Azure IoT Hub の イベントハブ互換エンドポイントに Node.js で subscribe


次のプログラムを改造して、イベントハブ互換エンドポイントを使うようにしました。
Azure IoT Hub に Node.js で subscribe
Hub に subscribe しているので、その Hub 内の Device に届いた総てのメッセージが表示されます。Device の認証が、SAS トークンでも、CA 証明書でも、どちらも表示されます。

azure_endpoint_subscribe.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  azure_endpoint_subscribe.js
//
//                  Jan/21/2021
//
// ---------------------------------------------------------------
'use strict'
const dotenv = require('dotenv')
const { EventHubConsumerClient } = require("@azure/event-hubs");

dotenv.config()

const endpoint_connectionString = `${process.env.ENDPOINT}`

var printError = function (err) {
  console.log(err.message)
};

var printMessages = function (messages) {
  for (const message of messages) {
    console.log("Telemetry received: ")
    console.log(JSON.stringify(message.body))
    console.log("")
/*
    console.log("Properties (set by device): ");
    console.log(JSON.stringify(message.properties));
    console.log("System properties (set by IoT Hub): ");
    console.log(JSON.stringify(message.systemProperties));
    console.log("");
*/
  }
}

// ---------------------------------------------------------------
async function main() {
  console.error("*** azure_endpoint_subscribe.js *** start ***")

  const clientOptions = {
  }

  const consumerClient = new EventHubConsumerClient("$Default", endpoint_connectionString, clientOptions)

  consumerClient.subscribe({
    processEvents: printMessages,
    processError: printError,
  })
}

main().catch((error) => {
  console.error("Error running sample:", error)
})

// ---------------------------------------------------------------
.env
ENDPOINT="Endpoint=sb://ihsuprodkwres017dednamespace.servicebus.windows.net/;SharedAccessKeyName=iothubowner;SharedAccessKey=vL1ze1abcdefgh48cMK7l6nv6+o37k2s9F70SD+LLZUs=;EntityPath=iothub-ehub-iot-bb-344012-58f0012d36"

Azure Portal で エンドポイントの値を取得