Are multiple # allowed in MQTT topics?

Viewed 947

Is it allowed to use multiple # in MQTT topics ?

For example:

sensors/data/#/something/else/#

2 Answers

No

The MQTT spec (v3 and v5) says a topic can only use the '#` once in a topic and it must be at the end. e.g.

sensor/data/foo/#

There is a second wildcard character which is +. This can be used anywhere in the topic (between /) and matches a single level of topic. e.g.

sensor/+/foo/+/json

This will match:

sensor/1/foo/temp/json
sensor/2/foo/humidity/json

but will not match:

sensor/1/bar/temp/json

or

sensor/+/+/data

which would match:

sensor/foo/bar/data

but not:

sensor/foo/bar/baz/data

You can also combine both e.g.

sensor/+/bar/#

which would match:

sensor/foo/bar/baz/wibble/json
sensor/bar/bar/black/sheep
Related