question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hub message routing on $body fails

See original GitHub issue

Using 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:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
LouanDuToitS3commented, Sep 6, 2019

Hi @BertKleewein, I forgot to add to the origional post that in another part of the code I serialise the JSON as following:

bytearray(json.dumps(msg), "utf-8")

So it is a bytearray that is sent.

0reactions
az-iot-builder-01commented, Sep 14, 2020

@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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found