Search code examples
eclipse-hono

Eclipse Hono MQTT adapter: MQTT topic mapping


How does events from arbitrary mqtt topics such as "base/context/qualifier/subtopic" fold into Hono MQTT adapter topic structure? Do you simply publish your event to topic: "event" and use the message body to set the context from the original topic?


Solution

  • There are several options available.

    1. If the topic that the device/client will publish to is configurable, then you have the following two options:

      1.1. Publish to topic event (or just e to save a few bytes per message) and include all relevant context information in the message payload so that the consumer can figure out the purpose of the message from the payload.

      1.2. Publish to a topic that looks like event/${tenant-id}/${device-id}/${custom-path}, where ${tenant-id} is the identifier of the tenant that the device belongs to and ${device-id} is the identifier of the device. The ${custom-path} can be any arbitrary path segment of your choice. The MQTT adapter will forward the MQTT message's payload in an AMQP message that has its address property set to event/${tenant-id} and that will contain an application-property called orig_address which will contain the full topic name that the message has originally been published to. A consumer can then use this information to derive contextual information like the message purpose. Refer to the MQTT adapter user guide for details.

    2. If the topic can not be configured on the device then you can still implement your own custom MQTT adapter. This sounds harder than it is in reality because most of the functionality required is already available in the org.eclipse.hono.adapter.mqtt.AbstractVertxBasedMqttProtocolAdapter base class which you can derive from and just implement the topic mapping logic. The Kura adapter is implemented in exactly this way and consists only of a few lines of code. Just take a look at org.eclipse.hono.adapter.kura.KuraProtocolAdapter.