7

I am wondering about best practices of topic naming and payload design of mqtt messages.

Is it better to have multiple and long topic names instead and a bigger payload or short topic name with bigger payload.

For example:

plant1/machineA/sensorX/temperature/value 20
plant1/machineA/sensorX/temperature/unit C
plant1/machineA/sensorX/temperature/timestamp 2018-08-01T12:00:30.123Z

vs.

plant1/machineA/
{
  ["sensorX": {
   "value": 20,
   "unit": "C",
   "timestamp": "2018-08-01T12:00:30.123Z"
  }]
}

There are a lot more possibilities. But is there a general approach? As much as possible in topic name or in payload?

1
  • 1
    It totally depends on your use case.
    – hardillb
    Aug 16, 2018 at 15:14

2 Answers 2

5

Your design question is that of a protocol on top of the MQTT transport. There are existing protocols that can guide your choice:

The choice for topic endpoints for high-scalability applications seems to be a small set of topics per thing (makes sense: if you have millions of things, you want to minimize the switching burden on the broker), at least one "event" topic (where telemetry is published from thing to application) and one "control" topic (where commands are received from the application by the thing).

Further, the information in the payload usually consists of a key (eg. timestamp or sequence number) and values for attributes. In your first choice, how do you tie together the different values received on different topics in the face of unsynchronized message streams over your network?

2
4

The decision should be made based on how are you using topics. If you need values together - post them into one topic, if you are using separately - put them into separate topics. Also do not post values, like in your first sample, in the topics. And do not create exceed topics, like sensorX/temperature for temperature sensors, go from more general in the beginning to more specific in the end. So I would recommend you to post into:

plant1/machineA/sensorX
{
   "value": 20,
   "unit": "C",
   "timestamp": "2018-08-01T12:00:30.123Z"
}

or if you have various types of sensors and want to select between them put temperature somewhere before sensor name:

plant1/machineA/temperature/sensorX

or also possible case is:

temperature/plant1/machineA/sensorX

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.