How can I configure to let HiveMQ allow dollar-topics?

Hi,

I found from here(https://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices/) that
“$” can not be used when you publish a message.
And indeed I found this from the code:
public static final AtomicBoolean MQTT_ALLOW_DOLLAR_TOPICS = new AtomicBoolean(false);

And I want to ask if I can change it to true to let HiveMQ allow dollar-topics?

Thank you.

Hi @chengxiangwang,

yes you can change MQTT_ALLOW_DOLLAR_TOPICS to true (and build your custom hivemq ce zip) then you can publish to $ topics with your clients.

Not sure what you want to do, maybe you can also benefit from the PublishService as this service can publish to $ topics without changing the internal config.

Greetings,
Michael from the HiveMQ team

1 Like

Thank you for the reply!
But I don’t want to rebuild it, can you tell me more detail of how to use the “PublishService”?

Hi @chengxiangwang,

sorry for the delayed response (I had holiday). Simple example for sending a publish via the publish service:

Publish message = Builders.publish()
    .topic("$topic")
    .qos(Qos.AT_LEAST_ONCE)
    .payload(payload)
    .build();

Services.publishService().publish(message);

I don’t know when you should use it (i.e. in what interceptor, etc.), as I don’t know your use case for using the dollar topic. If you need help here you should describe your use case.

Here is the link to the full publish service documentation: https://www.hivemq.com/docs/hivemq/latest/extensions/services.html#publish-service

Kind regards,
Michael