Publish Outbound Interceptor

Hi,

I’m new to HiveMQ and have a few questions regarding Publish Outbound Interceptor:

I got to know from the documentation that we can use Publish Outbound Interceptor to prevent the delivery of a PUBLISH message at the moment it is sent to its subscriber.

  1. Is there a way to retrieve all the intended recipients/subscribers of this message at this point?

  2. If this message has multiple subscribers, does preventPublishDelivery() of PublishOutboundOutput prevent the message delivery to all its subscribers?

I’m looking to write an extension that intercepts every message going out from HiveMQ broker to the subscribers and prevents the message delivery to certain subscribers only.

  1. Can this be done using preventPublishDelivery() of PublishOutboundOutput? Please could anyone shed some light on this?

Thank you!

Kind regards,
SpringD

Hi SpringD,

Welcome to the HiveMQ Community Forum and thanks for your interest in developing extensions.

All interceptors are called on a “per client” basis. That means for the PublishOutboundInterceptor that it would be called for every subscriber of the topic.

If you call preventPublishDelivery() it will only affect the client for which the interceptor was called.
To find out which client it is, you can check the client id by calling getClientInformation().getClientId() on the PublishOutboundInput

Here a simple example interceptor which prevents every publish to topic “not/for/A” for clients that contain an “A” in there clientID.

final PublishOutboundInterceptor myInterceptor = new PublishOutboundInterceptor() {
            @Override
            public void onOutboundPublish(final @NotNull PublishOutboundInput publishOutboundInput, final @NotNull PublishOutboundOutput publishOutboundOutput) {
                final String clientId = publishOutboundInput.getClientInformation().getClientId();
                final String topic = publishOutboundInput.getPublishPacket().getTopic();
                if (clientId.contains("A") && topic.equals("not/for/A")) {
                    log.info("publish prevented for client '{}' and topic '{}'", clientId, topic);
                    publishOutboundOutput.preventPublishDelivery();
                    return;
                }
                log.info("publish delivered for client '{}' and topic '{}'", clientId, topic);
            }
        };

For more information about the interceptors and the extension SDK, there is a documentation on our website:
https://www.hivemq.com/docs/hivemq/latest/extensions/interceptors.html#publish-outbound-interceptor

I hope this will help you.

Kind regards,
Flo

1 Like

Hi Flo,

Thanks very much for your prompt reply!

Can I double check if I understand correctly?

  1. Calling getClientInformation().getClientId() on the PublishOutboundInput returns the subscriber id ?

  2. Calling getClientInformation().getClientId() on the PublishInboundInput returns the publisher id ?

Thanks!

1 Like

Hi SpringD,

yes this is correct :slightly_smiling_face: !

Kind regards,
Flo

1 Like

Hi,

I was wondering if there is a way to get the ID of the sender/publisher of a packet onOutboundPublish?

Thanks!

Kind regards,
SpringD

Hi @SpringD,

this is currently not possible out of the box. There is one workaround that you could use though:

  • in a PublishInboundInterceptor: Add user property “clientID” to the publish with the client id of the client that sent the publish
  • in a PublishOutboundInterceptor: Read and then remove the user property “clientID” of the publish

Hope that helps!

Greetings,
Michael

1 Like

Hi Michael,

Thanks for your reply! That’s what I’m doing :slight_smile: