3
votes

I use Azure Service Bus to communicate IoT Hub with Node.js Functions. On Function side, I need to have access to message body as well as custom properties.

By retrieving messages elsewhere, I have noticed that my message in Service Bus consists of:

  1. Body (the string set on IoT Device side).
  2. Custom properties (include properties set on IoT Device side, IoT Device ID, and some additional metadata).

I have found online that in C# it is possible to access these custom properties using BrokeredMessage object. However, there is no mention on how to make this happen in Node.js. To give some details, I am printing the message as soon as it arrives to Function, using the following code:

module.exports = function(context, message) {
    context.log('Message', message, 'of type', (typeof message));
    ...
}

What I am getting in the log console is:

message { test: true } of type object

Where "{ test: true }" is the content of a message set by IoT Device. No trace of properties though...

Is there some formal way or at least a trick to receive and extract these properties?

Thank you!

1
This is not supported at the moment. Kindly see stackoverflow.com/a/42394345/6465830Ling Toh
@LingToh it actually is! Thank you for the reference - I have posted the answer below.Paweł Skorupiński
Cool, glad that you are unblocked now!Ling Toh

1 Answers

3
votes

Browsing resources mentioned in the link posted by Ling Toh in a comment I got inspired to have a look at the context object.

Apparently, all of the Service Bus custom properties are available in context.bindingData.properties object.

In my case:

properties: 
{ 
    type: 'sometype',   // <- this is the property I have set manually in IoT Hub message
    'iothub-connection-device-id': 'mydeviceid',
    'iothub-connection-auth-method': '{"scope":"somescope","type":"sometype","issuer":"external","acceptingIpFilterRule":null}',
    'iothub-connection-auth-generation-id': 'someid' // <- These are added by IoT Hub
}