Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9350

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

$
0
0

次のプログラムを改造して、イベントハブ互換エンドポイントを使うようにしました。
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'constdotenv=require('dotenv')const{EventHubConsumerClient}=require("@azure/event-hubs");dotenv.config()constendpoint_connectionString=`${process.env.ENDPOINT}`varprintError=function(err){console.log(err.message)};varprintMessages=function(messages){for(constmessageofmessages){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("");
*/}}// ---------------------------------------------------------------asyncfunctionmain(){console.error("*** azure_endpoint_subscribe.js *** start ***")constclientOptions={}constconsumerClient=newEventHubConsumerClient("$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 で エンドポイントの値を取得

endpoint_jan21.png


Viewing all articles
Browse latest Browse all 9350

Trending Articles