0

I'm trying to avoid the Azure SDK.

I am trying to connect to Azure IoT Hub with async-mqtt npm package.

I can successfully manually publish to default devices/${deviceId}/messages/events/ topic using just async-mqtt and I can easily read the published data with Azure Quickexample (https://github.com/Azure-Samples/azure-iot-samples-node/archive/master.zip).

When I try to read data just with async-mqtt I can't get any data (Following is the code I'm using).

        await client.publish(`devices/${deviceId}/messages/events/`, "testing");
        await client.subscribe(`devices/${deviceId}/messages/devicebound/`); 
        client.on('message', function (topic, message){
            console.log("Received"); 
            console.log(message.toString()); 
        }); 
        await client.end(); 

I can't get what I'm doing wrong.

Edit: This is the client configuration

var client = MQTT.connect(`mqtts://${iotHubName}.azure-devices.net:8883`, {
  keepalive: 10,
  clientId: MyNodeDevice,
  protocolId: 'MQTT',
  clean: true,
  protocolVersion: 4,
  reconnectPeriod: 1000,
  connectTimeout: 30 * 1000,
  username: userName,
  password:"SAS TOKEN",
  rejectUnauthorized: false, 
});
  • You have not included any information about how you are connecting to the Azure broker and that last line will disconnect you immediately. – hardillb Nov 15 '19 at 16:11
  • @hardillb added the configuration. I tried removing the client.end() but it just stands. It doesn't receive the message just published. I also tried switching publish and subscribe order, subscribing first then publishing. – DoodlePain Nov 15 '19 at 16:36
  • Can you confirm what you mean by "can't get any data"? As a reminder IoT Hub is not a full-featured MQTT broker so the "subscribe" you're making is for listening to commands potentially sent using the IoT Hub API and/or Service SDK, it is not meant to allow you to receive messages published by other devices connected to the same hub. – kartben Nov 15 '19 at 19:32
  • The fact that in a previous comment you say " It doesn't receive the message just published." makes me think that you are indeed expecting to receive messages coming from your/other devices. You can read more on supported workflow here: https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-mqtt-support#receiving-cloud-to-device-messages – kartben Nov 15 '19 at 19:34

0 Answers0