# Strange MQTT Client behavior

**URL:** <https://community.homey.app/t/strange-mqtt-client-behavior/45105>\
**Category:** Apps\
**Tags:** flow, mqtt\
**Created:** [March 22, 2021, 7:03pm UTC](https://community.homey.app/t/strange-mqtt-client-behavior/45105 "2021-03-22T19:03:01Z")\
**Posts on this page:** 1\
**Showing post:** 65

<div class="post-metadata">

**Author:** ![HarriedeGroot](https://sea1.discourse-cdn.com/flex025/user_avatar/community.homey.app/harriedegroot/32/56160_2.png) [@HarriedeGroot](https://community.homey.app/u/HarriedeGroot)\
**Post date:** [April 18, 2021, 10:07am UTC](https://community.homey.app/t/strange-mqtt-client-behavior/45105/65 "2021-04-18T10:07:19Z")

</div>

For completeness, the updated [test version](https://homey.app/nl-nl/app/nl.scanno.mqtt/MQTT-Client/test/) can be found here:

> **[MQTT Client](https://homey.app/nl-nl/app/nl.scanno.mqtt/MQTT-Client/)**
>
> Abonneer op MQTT topics

Please report your findings 🙂

Details of the changes:

> <https://github.com/scanno/nl.scanno.mqtt/pull/28>
>
> 1. \*\*Made some improvements to the topic registrations (api vs trigger)\*\*
> 2. \*\*…Fixed handling of retained messages\*\*
> 3. \*\*Prevent losing api connections\*\*
> 4. \*\*Addressed the bug of randomly disabled flows\*\*
> 5. \*\*Added unsubscribe api\*\*
> 6. \*\*Introduced a TriggerQueue to handle the rate limiting of flow card triggers and prevent disabling of flows\*\*
> 7. \*\*Added broker online/offline flow card triggers\*\*
> 8. \*\*Added flow card action to send an empty message to be able to remove retained messages from the broker\*\*
> 9. \*\*Introduced TopicsRegistry to register topics by reference (e.g. app id), this reference can be used with the unsubscribe api\*\*
> 10. \*\*Performance improvements\*\*
> 11. \*\*General bug fixes\*\*
> 
> \*\*Commit 1\*\*
> Topic registration:
> \- always subscribe to the broker to return retained messages on subscription
> \- re-subscribe to all previously registered topics on connection of the broker/mqtt-client (keep topic registry alive)
> \- made some calls async and actually wait for the result before continuing + fixed some async handling of promises
> \- extended the TopicArray with seperate methods for api & trigger topics
> 
> \*\*Commit 2\*\*
> \- prevent topic unsubscription if its used by another registration (api vs trigger)
> 
> \*\*Commit 3\*\*
> Flow card triggers:
> \- prevent destruction of the full topics array, causing all api connections being lost
> \- subscribe to all flow card trigger topics on initialisation
> \- on trigger card change (flow card config): unsubscribe from removed topics & subscribe to newly added topics
> \- await the Homey trigger call
> \- check for api topic match before forwarding the message to the realtime api
> 
> \*\*Commit 4\*\*
> pre-check to prevent uneccesairy calls to the Homey trigger system
> prevents disabling of random flows not even listening to these topics (Homey BUG?)...
> this is also a performance boost, since the Homey trigger system won't be called for all api topics
> the small downside is the duplicate topic matching, if there is a match on a trigger topic
> 
> \*\*Commit 5\*\*
> (added missing file 'Trigger.js' for commit 3
> 
> \*\*Commit 6\*\*
> On change of app settings:
> \- prevent destruction of the full topicsArray resulting in lost api connections
> \- re-connect to the broker to trigger subscription of all registered topics (instead of the trigger topics only)
> \- await topic unsubscription before setting up the new client
> 
> \*\*Commit 7\*\*
> added unsubscribe api
> 
> \*\*Commit 8\*\*
> Introduced a TriggerQueue to handle the rate limiting of flow card triggers and prevent disabling of flows
> 
> \*\*Commit 9\*\*
> Added broker online/offline flow card triggers
> 
> \*\*Commit 10\*\*
> Added flow card action to send an empty message to be able to remove retained messages from the broker
> 
> \*\*Commit 11\*\*
> Introduced a TopicsRegistry to register topics by reference (e.g. app id). The unsubscribe api can only be called for topics with the reference provided during topic subscription. This prevents unsubscription of topics used by other apps/processes.
> Also optimized the topic matching by using a topic map (instead of array lookup).
> 
> \*\*Commit 12\*\*
> \- Remove messages from the trigger queue when a topic is unsubscribed
> \- Added failsafe for the TriggerQueue size. Allow homey to disable the flow (by rate limiting) if the number of messages in the trigger queue exceeds the max allowed number of messages.
> \- Correctly handle topic subscriptions after flow card triger changes.
> \- updated the README with examples of usage of the reference and unsubscribing of topics
> 
> \*\*Commit 13\*\*
> \- merged api changes from commit: https://github.com/scanno/nl.scanno.mqtt/commit/02c3f0f816dd70475ebe2d848cd5012fb5034f2b
> \- added response to unsubscribe api
> \- version bump to 3.x

---

_[View the full topic](https://community.homey.app/t/strange-mqtt-client-behavior/45105)._
