node.jsazureazure-iot-hubazure-node-sdk

How to add a message property in node azure iot sdk


With the azure C sdk it's possible to add properties to the message envelope using Map_AddOrUpdate(propMap, "propKey", propText). Is there something similar? I'm using

const Protocol = require('azure-iot-device-mqtt').Mqtt;
const Client = require('azure-iot-device').Client;
const Message = require('azure-iot-device').Message;

client = Client.fromConnectionString(deviceConnectionString, Protocol);
// misc code
 return new Promise( (resolve,reject) => {
    client.sendEvent(message,(err,res) => {
      if ( err ) {
        logger.error(TAG,'Failed to send message: ', err.toString())
        reject(err)
      }
      if (res) logger.info(TAG,'Send status: ' + res.constructor.name);
      resolve(res)
    })
  })

I was unable to find anything in the client interface that allowed me to set envelope properties.


Solution

  • You can do: message.properties.add('yourProp', 'val'). You might find this sample useful.