Good way to design mqtt topic?

你离开我真会死。 提交于 2020-08-24 15:49:56

问题


I am very new with mqtt design.

As I see from some tutorials in the internet, common mqtt topic has this format: /home/room/device_type/device_id

I could not see the benefit to do that. And have no idea how to use this kind of design.

From my point of view, the device (dev) might subscribe (sub) to control topic and publish (pub) to status topic. Like this:

  • pub: clients/dev/devid/stat
  • sub: clients/dev/devid/ctrl

In this way, it seems sub,pub logic is very simple for both clients and devices

Could someone please tell me some good way to design mqtt topic ?

(!) Please do not start topic with '/' (This one has been recommended by HiveMQ Team)

EDIT:

I just figured out that for whatever kind of design, the model must serve-able at least:

  1. Individual control: send control command to a particular device.
  2. Group control: send control command to a group of devices: type, defined group
  3. Able to recieve the status of device.

Thank you very much


回答1:


I found that the following topic split scheme works very well in multiple applications

protocol_prefix / src_id / dest_id / message_id / extra_properties
  • protocol_prefix is used to differentiate between different protocols / application that can be used at the same time
  • src_id is the ID of the mqtt client that publishes the message. It is expected to be the same as "client ID" used to connect to MQTT broker. It allows quick ACL control to check whether the client is allowed to publish specific topic.
  • dest_id is client ID of the "destination" unit, i.e. to whom the message is intended. Also allows quick ACL control on the broker of whether client is allowed to subscribe to a particular topic. There can be reserved "destination" strings to specify that the message is broadcasted to anyone who is interested. For example all.
  • message_id is actual ID of the message within used protocol. I usually use numeric value (as string of course), because the IOT or other embedded system that is connected to MQTT broker can have other I/O links and I would like to use the same protocol (but with different transport framing) to control the device using these other I/O links. I usually use numeric message IDs in such communication links.
  • extra_properties is an optional subtopic which can be used to communicate other MQTT specific extra information (comma separated key=value pairs for example). Good example would be reporting timestamp of the message when it was actually sent by the client. In case of "retained" messages it can help to identify the relevance of the received message. With MQTTv5 protocol that is expected to arrive soon, the need for this subtopic may disappear because there will be other way to communicate extra properties.

Hope it helps.




回答2:


I think if topics are to reflect the physical world, we should look at something like Signal K.

EDIT: That spec is also still maturing, but it includes concepts like "self" for the server/broker, and a tree that can start at the current vessel/home, but easily extends upwards to other vessels/aircraft/things.

My two cents:

  1. All topics are read-only unless they end in "/set"
  2. Ideally, topics are reasonably normalized and granular. I can understand grouping values up into a group topic. IMHO, this kind of decision should be application-specific.
  3. Payloads should be strings, to avoid endian-ness issues

Here's one suggested tree:

  • broker = information of this specific broker
    • broker/clients
    • broker/clients/count
    • broker/clients/0/name or broker/clients[0]/name
    • broker/topics
  • home = this current location (could also be "here" or something)
    • home/kitchen/temperature "19C"
    • home/kitchen/temperature/hardware/type "ESP8266"
    • home/garage/maindoor/set "closed"
  • locations = list of all known locations
    • locations/0/uuid
    • locations/0/name
    • locations/0/address



回答3:


We have done some work on that for the domain of manufacturing (Industrial-IoT, not IoT!).

In our scenario there are lots of serverside apps of different companies communicating through MQTT. Thus we needed some overall structure. We call this "Manufacturing Message Stack".

The bottom layer is MQTT, then there is the "Messaging Layer". It consists mainly of

  • basic topic specs
  • basic payload specs

On top of the messaging layer there are domain message layers covering various domain specific topics as system messages, alerting, physical device / digital twin messages or other manufacturing related messages.

Topics

The topics are roughly defined as <senderapp>/<app-id>/<message-name>/<args> e.g. pacman/pacman-1/gameover (this is a sample for illustration only!)

The developer of an app which publishes MQTT messages defines <message-name> and <args> depending on the semantics of the payload.

The <senderapp> and <app-id> refers to the sending App and allows to quickly select messages from a defines origin (publisher). We deploy Apps in a microservice environment built with Docker, Rancher - and soon - Kubernetes.

Payload

The payload is specified in JSON format. There is a JSON schema reference URL in each build an URL to an API of the publishing app which holds further information (e.g. JSON schema) of the message sent. Thus a subscriber can get meta data of a MQTT message on demand. Static meta data is not sent with a message to reduce the payload size.

Payload sample:

{
    "$schema": "http://app/api/messages/message1.json",
    "score": 1234,
    "highscore": false
}

Publisher's message meta data

The publishing app holds an index of all messages which can be sent in an API at http://<app>/api/messages/index.json:

{
    "message1": "message1.json",
    ...
}

Each message is described by its JSON schema message1.json:

{
    "$schema": "http://json-schema.org/draft-06/schema#",
    "title": "Pacman end of game",
    "properties": {
        "score": {
            "description": "Players score at the end of game",
            "type": "integer"
        },
        ...
    }
}

Unfortunately we did not publish our manufacturing message stack yet. Publishing is planned in the next months. Feedback is welcome.



来源:https://stackoverflow.com/questions/48238365/good-way-to-design-mqtt-topic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!