# Publish Outbound Interceptor

**URL:** <https://community.hivemq.com/t/publish-outbound-interceptor/304>\
**Category:** HiveMQ Extension SDK\
**Created:** [August 17, 2020, 6:35pm UTC](https://community.hivemq.com/t/publish-outbound-interceptor/304 "2020-08-17T18:35:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SpringD](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@SpringD](https://community.hivemq.com/u/SpringD)\
**Post date:** [August 17, 2020, 6:35pm UTC](https://community.hivemq.com/t/publish-outbound-interceptor/304/1 "2020-08-17T18:35:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![FloLi](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/floli/32/17_2.png) [@FloLi](https://community.hivemq.com/u/FloLi)\
**Post date:** [August 18, 2020, 7:19am UTC](https://community.hivemq.com/t/publish-outbound-interceptor/304/2 "2020-08-18T07:19:42Z")

</div>

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](https://www.hivemq.com/docs/hivemq/latest/extensions/interceptors.html#publish-outbound-interceptor)

I hope this will help you.

Kind regards,  
Flo

---

<div class="post-metadata">

**Author:** ![SpringD](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@SpringD](https://community.hivemq.com/u/SpringD)\
**Post date:** [August 18, 2020, 10:53am UTC](https://community.hivemq.com/t/publish-outbound-interceptor/304/3 "2020-08-18T10:53:19Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![FloLi](https://yyz1.discourse-cdn.com/flex035/user_avatar/community.hivemq.com/floli/32/17_2.png) [@FloLi](https://community.hivemq.com/u/FloLi)\
**Post date:** [August 18, 2020, 11:05am UTC](https://community.hivemq.com/t/publish-outbound-interceptor/304/4 "2020-08-18T11:05:50Z")

</div>

Hi SpringD,

yes this is correct 🙂 !

Kind regards,  
Flo

---

<div class="post-metadata">

**Author:** ![SpringD](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@SpringD](https://community.hivemq.com/u/SpringD)\
**Post date:** [December 7, 2020, 7:32pm UTC](https://community.hivemq.com/t/publish-outbound-interceptor/304/5 "2020-12-07T19:32:17Z")

</div>

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

---

<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:** [December 8, 2020, 8:18am UTC](https://community.hivemq.com/t/publish-outbound-interceptor/304/6 "2020-12-08T08:18:55Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SpringD](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@SpringD](https://community.hivemq.com/u/SpringD)\
**Post date:** [December 8, 2020, 9:14am UTC](https://community.hivemq.com/t/publish-outbound-interceptor/304/7 "2020-12-08T09:14:22Z")

</div>

Hi Michael,

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