Hi,
is there any chance to let HiveMQ send out a given message to a specific client only?
I know this is not covered by the MQTT standard and there are better ways to implement such use cases (like the MQTT 5 Request/Response Pattern), but I would like to replace an existing MQTT server that does not properly conform to MQTT patterns with HiveMQ without modification of the clients.
Nice to see your interest in MQTT and HiveMQ, welcome to the community!
In MQTT, whatever client subscribed to a topic will receive messages published on that topic. In order for only one client to receive messages, you can authorize only that particular client to subscribe to the topic and reject authorization for all other clients. This way only one client will subscribe to the topic and hence only that client will receive messages published on the topic. In MQTT, this is called Topic Authorization.
Authorization in HiveMQ Community Edition broker can be set up with help of the HiveMQ File RBAC Extension that is open-source.
Hi Dasha,
thanks a lot! Using topic authorization for such a scenario is a great idea! Unfortunately all my clients subscribe to the same topic. So I will have a deeper look if it is realizable with an extension.
Hi Dasha,
writing an extension to do this job was surprisingly simple as shown below. Of course, some details need to be sorted out for our specific use case, but the main problem is solved. Thanks a lot!
Greetings,
Florian
@Override
public void onOutboundPublish(
final @NotNull PublishOutboundInput input,
final @NotNull PublishOutboundOutput output) {
final String clientId = input.getClientInformation().getClientId();
final String topic = input.getPublishPacket().getTopic();
if (topic.startsWith("topsecret") && !clientId.startsWith("jamesbond")) {
output.preventPublishDelivery();
}
}
@koalo ,
very happy you found a way to implement the use case and thank you for sharing your solution – I hope it will be useful for our future community members