Hub message routing on $body fails
See original GitHub issueUsing the new v2 API, I am unable to route messages using the $body tag when sending module to module messages.
In the code I send messages as following:
def _send_message(self, message, output_name):
"""Create the message object and send to the requested output."""
try:
message_object = Message(message, content_encoding="utf-8", content_type="application/json")
self._module_client.send_message_to_output(message_object, output_name)
except Exception:
self.logging_handler.exception("Exception sending message to output")
The message itself will be JSON serialised and here are some samples:
{'time': 1565009829.844609, 's_time': '2019-08-05T12:57:09', 'routing_key': 'tic_tock'}
{'time': 1565009829.844609, 's_time': '2019-08-05T12:57:09', 'routing_key': 'tic_knock'}
{'time': 1565009829.844609, 's_time': '2019-08-05T12:57:09', 'routing_key': 'tock'}
{'time': 1565009829.844609, 's_time': '2019-08-05T12:57:09', 'routing_key': 'tock_tic'}
{'time': 1565009829.844609, 's_time': '2019-08-05T12:57:09', 'routing_key': 'knock'}
The filter rules in my deployment manifest is:
"$edgeHub": {
"properties.desired": {
"routes": {
"send-to-tic": "FROM /messages/modules/hubtester_send/outputs/output1 WHERE CONTAINS($body.routing_key, 'tic') INTO BrokeredEndpoint(\"/modules/hubtester_receive_tic/inputs/input1\")",
"send-to-toc": "FROM /messages/modules/hubtester_send/outputs/output1 WHERE CONTAINS($body.routing_key, 'toc') INTO BrokeredEndpoint(\"/modules/hubtester_receive_toc/inputs/input1\")"
},
"schemaVersion": "1.0",
"storeAndForwardConfiguration": {
"timeToLiveSecs": 60
}
}
},
Using the sample strings as listed above, none of the messages are delivered.
But, as a workaround, if I now change the code to use the messages “custom_properties”, like this:
def _send_message(self, message, output_name, routing_key):
"""Create the message object and send to the requested output."""
try:
message_object = Message(message, content_encoding="utf-8", content_type="application/json")
if routing_key is not None:
message_object.custom_properties["routing_key"] = routing_key
self._module_client.send_message_to_output(message_object, output_name)
except Exception:
self.logging_handler.exception("Exception sending message to output")
And then call the send_message
method with the reouting_key I need, and with the following routing rules in my manifest:
"$edgeHub": {
"properties.desired": {
"routes": {
"send-to-tic": "FROM /messages/modules/hubtester_send/outputs/output1 WHERE CONTAINS(routing_key, 'tic') INTO BrokeredEndpoint(\"/modules/hubtester_receive_tic/inputs/input1\")",
"send-to-toc": "FROM /messages/modules/hubtester_send/outputs/output1 WHERE CONTAINS(routing_key, 'toc') INTO BrokeredEndpoint(\"/modules/hubtester_receive_toc/inputs/input1\")"
},
"schemaVersion": "1.0",
"storeAndForwardConfiguration": {
"timeToLiveSecs": 60
}
}
},
Then all is fine.
I tried not using the “CONTAINS” filter and just a exact “plain =” match, but that did not work either.
I would like to use message body routing.
AB#7366698
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
IoT Hub Message routing - $body not working - Microsoft Q&A
Hi, I have an IoT hub device which sends messages via message routing to a bus queue and then to a logic app...
Read more >Azure IotHub routing is not functioning - Stack Overflow
Different types of error messages started to appear like IotHub is not in an Active state whatsoever. Deleting and recreating the resource ...
Read more >Azure IOT Hub message routing: Filter on device twin tags not ...
We have tried several times to filter the message based on a combination of a device twin property and a message body property....
Read more >IoT Hub: Routing Messages to Different Destinations
1 IoT Hub: What it is; 2 The problem; 3 Message routing to the rescue ... but they are also capable of using...
Read more >Routing Messages in Azure IoT Hub based on Device Twin
Check out this great demo of the new way to configure message routing in Azure IoT Hub using Device Twin properties and the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @BertKleewein, I forgot to add to the origional post that in another part of the code I serialise the JSON as following:
So it is a bytearray that is sent.
@BertKleewein, @LouanDuToitS3, thank you for your contribution to our open-sourced project! Please help us improve by filling out this 2-minute customer satisfaction survey