Azure / azure-iot-sdk-node

A Node.js SDK for connecting devices to Microsoft Azure IoT services
https://docs.microsoft.com/en-us/azure/iot-hub/
Other
261 stars 227 forks source link

Sending C2D Message to a Module fails #1007

Closed shaneholder closed 3 years ago

shaneholder commented 3 years ago

Hello,

"name": "azure-iot-device" "version": "1.17.4"

I am attempting to send a message from a C# process to a NodeJS ModuleClient which uses the MQTT transport.

This is the relevant C# code that is sending the message.

await s_serviceClient.SendAsync("mydevice", "hello", new Message(){MessageId="Hi jody", To = "fred"});

A corresponding C# client can receive the message with:

s_moduleClient.SetMessageHandlerAsync((m, c) =>
          {
            _logger.LogInformation($"received default message SetMessageHandlerAsync {m.MessageId}");
            return Task.FromResult(MessageResponse.Completed);
          }, null);

I am attempting to receive the message in Node using:

client.on('inputMessage', (m) =>{})

I have turned on debugging

$env:DEBUG="azure-iot-device:* azure-iot-device-mqtt:* "

And I am seeing that the message I am sending is making it to the MQTT transport layer but is not matching any of the provided _topics.

The topic being received is:

devices/mydevice/modules/hello/messages/devicebound/%24.mid=Hi%20jody&%24.to=%2Fdevices%2Fsah-rfidprinter1%2Fmodules%2Fhello%2Fmessages%2FdeviceBound

I am also sending a command from the C# process which is received by the node process.

 var response = await s_serviceClient.InvokeDeviceMethodAsync("mydevice", "hello" , methodInvocation);

Receiving code:

client.onMethod(commandMaxMinReport, commandHandler);

I guess my question is how can I send a C2D to a ModuleClient so that it can be received?

In my debugging it seemed that the message topic matched the else condition around mqtt.js:766 so I commented out the else condition so that the message property would be added to _topics which matched. I then found that when the C2D handler was run an emitter was fired with 'message' which was not emitted by module_client.js so I just replicated the inputMessage code in module_client.js and sure enough I could now catch a 'message' in my node process code. I'm sure this is not the correct behavior but I just wanted to report what I was able to successfully achieve.

Thanks, Shane

anthonyvercolano commented 3 years ago

@shaneholder Are you saying that the method invocation worked and the c2d did not?

YoDaMa commented 3 years ago

@shaneholder I'm curious if you change

client.on('inputMessage', (m) =>{})

to

client.on('message', (m) =>{})

are you communicating IoT Hub to Module Client or Edge Hub to Module Client? It looks like with a service client set up in C# you're communicating directly

Service Client ----> IoT Hub ----> Module Client

shaneholder commented 3 years ago

@anthonyvercolano

Yes, that is what I experienced, c2d messages would not be received by the client but c2d methods would. I did see the C2D message show up in MQTT on the client, but it just did not match the topic.

@YoDaMa

My scenario is regular IOT not IOT Edge or Hub. I'll see if I can setup the test again and find out.

I also find it interesting that in Azure IoT Explorer and the Azure IOT Portal that you can send a Message to the Device, however this option is not offered on Modules, is that just a feature that needs to be added to the portal/IoT Explorer or are we not supposed to send messages to Modules.

As a user I don't (yet) see a distinction in purpose between the Device and a Module. I am building a collection of services that will work together on a single piece of hardware so I want them to all have the same DeviceId however I want them to act as independent units so I'm creating Modules so I can get a different connection string and not run into the issues with having multiple connections for MQTT.

Perhaps I'm not thinking about Modules correctly?

Regards, Shane

shaneholder commented 3 years ago

I put together a gitrepo to demonstrate the issue I am seeing

https://github.com/shaneholder/iotDeviceMessageIssues

Hopefully this will illustrate the problem.

YoDaMa commented 3 years ago

@shaneholder replying in the other thread, since your questions are converging. https://github.com/Azure/azure-iot-sdk-node/issues/1016

YoDaMa commented 3 years ago

closing this issue since it's being addressed in a different thread.