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

**URL:** <https://community.hivemq.com/t/how-to-subscribe-to-a-certain-topic-in-my-hivemq-extension/537>\
**Category:** HiveMQ Extension SDK\
**Created:** [May 19, 2021, 5:06pm UTC](https://community.hivemq.com/t/how-to-subscribe-to-a-certain-topic-in-my-hivemq-extension/537 "2021-05-19T17:06:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![janosch](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@janosch](https://community.hivemq.com/u/janosch)\
**Post date:** [May 19, 2021, 5:06pm UTC](https://community.hivemq.com/t/how-to-subscribe-to-a-certain-topic-in-my-hivemq-extension/537/1 "2021-05-19T17:06:41Z")

</div>

**Hi guys,**

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

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 … 😉):

 ![Bildschirmfoto vom 2021-05-19 21-39-37](https://canada1.discourse-cdn.com/flex035/uploads/hivemq1/original/1X/214749ef943bddf09e4a7f78726fb4c5fa2cd38a.png)

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

---

<div class="post-metadata">

**Author:** ![michael\_w](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/michael_w/32/200_2.png) [@michael\_w](https://community.hivemq.com/u/michael_w)\
**Post date:** [May 20, 2021, 11:39am UTC](https://community.hivemq.com/t/how-to-subscribe-to-a-certain-topic-in-my-hivemq-extension/537/2 "2021-05-20T11:39:56Z")

</div>

Hi @janosch,

I think what you are looking for is the [consumer service](https://www.hivemq.com/docs/hivemq/latest/extensions/services.html#consumer-service). But this feature doesn’t come with the normal SDK but with the [Enterprise SDK](https://www.hivemq.com/docs/hivemq/latest/extensions/services.html#enterprise-sdk-services).

The alternative would be as you already mentioned the PublishInboundInterceptor (or [PublishAuthorizer](https://www.hivemq.com/docs/hivemq/latest/extensions/authorization.html#publish)). 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](https://www.hivemq.com/docs/hivemq/latest/extensions/services.html#managed-extension-executor).

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

```auto
 @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

---

<div class="post-metadata">

**Author:** ![janosch](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@janosch](https://community.hivemq.com/u/janosch)\
**Post date:** [May 21, 2021, 10:32am UTC](https://community.hivemq.com/t/how-to-subscribe-to-a-certain-topic-in-my-hivemq-extension/537/3 "2021-05-21T10:32:58Z")

</div>

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 … 🥴

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

---

<div class="post-metadata">

**Author:** ![hivemq-support](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/hivemq-support/32/357_2.png) [@hivemq-support](https://community.hivemq.com/u/hivemq-support)\
**Post date:** [May 21, 2021, 5:46pm UTC](https://community.hivemq.com/t/how-to-subscribe-to-a-certain-topic-in-my-hivemq-extension/537/4 "2021-05-21T17:46:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![janosch](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@janosch](https://community.hivemq.com/u/janosch)\
**Post date:** [June 6, 2021, 2:23pm UTC](https://community.hivemq.com/t/how-to-subscribe-to-a-certain-topic-in-my-hivemq-extension/537/5 "2021-06-06T14:23:50Z")

</div>

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
