MQTT Topics Design: More or Less Topics

71 views
Skip to first unread message

Reuben Rebullar

unread,
Dec 8, 2017, 1:50:14 PM12/8/17
to MQTT
Is it better to have more or less topics? 

For example:

I have 3 features, turn ON/OFF LED1, Turn ON/OFF LED2, Turn ON/OFF LED3.

I can either create 3 topics

topics:
device/LED1
device/LED1
device/LED1

topic payload:
led_status: 
   0 = OFF
   1 = ON



OR

Create 1 Topic

topics:
device/LED

topic payload:

topic payload:
led_number
   range of 0-2

led_status: 
   0 = OFF
   1 = ON

Thanks,
Reuben

Frank Pagliughi

unread,
Dec 9, 2017, 11:58:33 AM12/9/17
to mq...@googlegroups.com, Reuben Rebullar
Generally it's best to consider the finest level to which an individual client might subscribe. There's no set rule, but if you put too much detail into the topic structure, then clients will need to subscribe to a lot of topics to get the messages they need. If you don't put enough detail, then clients will be receiving a lot of messages that they don't  want.

In your case it appears that you're using messages to send commands to the devices, and that each device has a bank of multiple LED's?

Assuming that's true, then each device will need to get all the message(s) no matter which LED. So, having separate messages for each LED would be redundant, and it would also require you to parse both the topic and the payload to figure out the required action - getting the LED # from the topic and the level from the payload.

I would use your second case. Allow each device subscribe to "device<#>/LED" and then get the LED number and level from the payload.

Frank
--
To learn more about MQTT please visit http://mqtt.org
---
You received this message because you are subscribed to the Google Groups "MQTT" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mqtt+uns...@googlegroups.com.
To post to this group, send email to mq...@googlegroups.com.
Visit this group at https://groups.google.com/group/mqtt.
For more options, visit https://groups.google.com/d/optout.

Phil Scanlon

unread,
Dec 10, 2017, 5:57:01 AM12/10/17
to MQTT
  You do not need to have multiple subscriptions if you want to listen to multiple devices/topics - the MQTT broker should support wildcard subscriptions at any level, and your topic hierarchy is more granular as you go down the levels.

. Each device should have it's own topic, and if there are sub devices, then create a deeper hierarchy..  Each device/led is then individually addressable without having to parse the payload.  you can apply security against devices based on a device ID in the topic.
Reply all
Reply to author
Forward
0 new messages