How to subscribe to a certain topic in my HiveMQ Extension?

Hi guys,

first of all: HiveMQ and especially it’s Extension SDK is a great piece of work!
Thank you for that! :clap:

Coming to my problem.
Currently I’m working on an extension, to perform cartesian-to-polar coordinate transformation forth and back. As an examle:

A Client proposes a cartesian x/y target position to a certain topic, let’s say:

client/setpoint: { "x": 0, "y": 0 }

A positioning device, which works with polar coordinates, subscribes to some other topics, for instance

dev/setpoint: { "alpha": 25.3, "phi":=45.0 }

Following sketch tries to make it a bit clearer (if not already … :wink:):

My first intention was writing an extension which simply subscribes to the client topic, performs transformation and then publishes to the device topics. Same way backwards to map actual values from the device to the client (for visualization).

Now, what I found out, there is no service in Extension SDK which gives the opportunity to subscribe to a certain topic. Publishing is possible via Services.publishService(). The only way so far I found is using a PublishInboundInterceptor on a per client basis, which goes “fishing” for the topics I require (by parsing them) …
I think, this is not a very resonable way and might introduce some performance issues.

What would be the preferred way to get the functionality I want?
Is there a reason, why subscriptions in an extension are not possible?
Should all what I want better be done by employing the mqtt-client?
(I’ve seen it is referenced in the pre-defined maven-archetype for extensions, but only for testing purposes … I guess for good reason?)

Best regards,
janosch

Hi @janosch,

I think what you are looking for is the consumer service. But this feature doesn’t come with the normal SDK but with the Enterprise SDK.

The alternative would be as you already mentioned the PublishInboundInterceptor (or PublishAuthorizer). I don’t think it will cause a big performance impact when you follow the “don’t block” principle, as in out-source the transformation + publish from the interceptor to the managed executor.

Simple example with the PublishAuthorizer (you can the also use the interceptor instead), I think that should point you in the right direction:

 @Override
    public void extensionStart(final ExtensionStartInput input, final ExtensionStartOutput output) {

        Services.securityRegistry().setAuthorizerProvider(authorizerProviderInput -> (PublishAuthorizer) (input1, output1) -> {
            final String topic = input1.getPublishPacket().getTopic();

            if (isPolarTopic(topic)) {
                //outsource to managed executor
                Services.extensionExecutorService().submit(() -> {
                    Publish transformedPublish = transformToCartesian(input1.getPublishPacket());

                    Services.publishService().publish(transformedPublish);
                });
            } else if (isCartesianTopic(topic)) {
                //outsource to managed executor
                Services.extensionExecutorService().submit(() -> {
                    Publish transformedPublish = transformToPolar(input1.getPublishPacket());

                    Services.publishService().publish(transformedPublish);
                });
            }

            output1.authorizeSuccessfully();
        });
    }

Greetings,
Michael from the HiveMQ team

2 Likes

Hi @michael_w,
thanks for your quick reply.

What you proposed is what I did so far (except that I indeed employed the PublishInboundInterceptor) and it works fine!

Regarding the ConsumerService you mentioned, you are absolutely right! That’s what I was looking for. Seems I have overlooked it, when I went through the SDK documentation … :woozy_face:

Though everything works fine what I did so far, I will nevertheless take a look at the Enterprise SDK for several reasons:

  • Coding gets much clearer and straight forward when using the ConsumerService
  • I have a simulation integrated into my extension which simulates my devices (final product will have around 20 of them) to reasonably test the client app. As those “simulators” are no real clients for the moment it made me some headache using InboundInterceptors (as they work on a per-client-basis) … but I got it running.

To clarify some questions I still have about this:

  1. Using the ConsumerService definitely involves usage of HiveMQ Enterprise Edition instead of HiveMQ-CE?
  2. What exactly is meant by the “certification”, which seems to be required for usage of the Enterprise SDK?
  3. How will the Enterprise SDK be employed? Is it a different maven project (i.e. archetype)?

Best regards,
janosch

Hi @janosch,

In regards to the HiveMQ Enterprise SDK

  1. The Enterprise SDK works with either HiveMQ Professional or Enterprise Edition, not HiveMQ Community Edition.
  2. HiveMQ customers can book a training and receive certification to use the Enterprise SDK
  3. The Enterprise SDK dependencies are available to certified HiveMQ extension developers via Maven or graddle.

Best,
Florian from The HiveMQ Team

1 Like

Hi @hivemq-support,
thanks for clarification.
I will let check pricing for HiveMQ Professional with sales dept.
Training can only get booked for customers (after having bought a license for either Professional or Enterprise Edition)?
And will there be an additional charge for the training?
Where can I find information about how the training is done?

Regards,
janosch