mqtt topic structure best practice; leading slash or not?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
kenkoknz
Normal user
Posts: 64
Joined: 03 Jul 2018, 23:46
Location: New Zealand

mqtt topic structure best practice; leading slash or not?

#1 Post by kenkoknz » 18 Aug 2018, 02:02

Hi,

Can some expert advice me re best and fail safe practice for constructing mqtt topic sentences.
My environment uses various wemos and sopoff loaded with mega using standard default OH mqtt protocol; hivemq and node-red , so far been good.
However I am a bit perplex, as all the topics all start leading slash (with default OH mqtt protocol).
Hivemq reccomends "never use leading slash" (https://www.hivemq.com/blog/mqtt-essent ... -practices), and recently a interesting forum topic in nodered with espeasy, highlighted the leading slash as the cause of mqtt connection problems. (https://discourse.nodered.org/t/wemos-d ... -mqtt/1960).
Just wanted to be sure that there are no potential issues with using leading slashes in a mqtt topic, as it is easy enough to leave the leading slash out if there are issues with it.

Thanks,
Ken

User avatar
grovkillen
Core team member
Posts: 3261
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: mqtt topic structure best practice; leading slash or not?

#2 Post by grovkillen » 18 Aug 2018, 12:01

According to the MQTT standard the slash is the delimiter of levels. By starting a topic with a slash you essentially tell the broker that the first level is named "null" (i.e. a non-existing value). This is not by any circumstances logical and should be avoided as well as double or triple slashes etc. should be avoided. So no, do not start with a slash. 8-)
Last edited by grovkillen on 20 Aug 2018, 06:29, edited 1 time in total.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

kenkoknz
Normal user
Posts: 64
Joined: 03 Jul 2018, 23:46
Location: New Zealand

Re: mqtt topic structure best practice; leading slash or not?

#3 Post by kenkoknz » 20 Aug 2018, 03:26

Hi @grovkillen,
Thanks for that, as most of the other protocols have leading /, perhaps might be handy to include in the protocol for controllers drop down, a generic mqtt protocal without leading / i.e:
%sysname%/#
%sysname%/%tskname%/%valname%
Cheers,
Ken

User avatar
grovkillen
Core team member
Posts: 3261
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: mqtt topic structure best practice; leading slash or not?

#4 Post by grovkillen » 20 Aug 2018, 06:28

OpenHAB is as close to a generic MQTT controller as we have... only removing the leading slash and your good to go. But yes, we need to have a "Generic MQTT" as we have with HTTP etc.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests