IOT MQTT Subscription Unreliable

ive been testing a MQTT Sub which run a on-message script, this seems to work fine for a few moments and then stops responding to the changes on the MQTT server.my setup is a RB4011 using an external MQTT server on the same network

/iot/mqtt/brokers/print
Flags: W - Will message enabled
0   name="broker" address="172.16.10.137" port=1883 ssl=no username="xxxxx" password="xxxxx" will-topic=""
will-message="" will-qos=0 will-retain=no auto-connect=no keep-alive=60 parallel-scripts-limit=off
connected=yes
/iot/mqtt/subscriptions/print
0 broker=broker topic="mkt/rb4011/vpn/status" qos=2
on-message=":log info "Got data {$msgData} from topic {$msgTopic}"\\n\\n:if ($msgData = "on") do={\\n
/ip/firewall/mangle/enable numbers=31\\n    :log info "VPN Status ON - Enabled mangle rule
31"\\n}\\n\\n:if ($msgData = "off") do={\\n    /ip/firewall/mangle/disable numbers=31\\n    :log info
"VPN Status OFF - Disabled mangle rule 31"\\n}"

You should not refer to the firewall rules by number, as the numbers are dynamic and intrinsically tied to the print command. This means that a firewall rule has nu number before you run the print command, and the number changes depending on what you do with the print command.

Instead, either refer to the firewall rule by the .id which is permanent, or use thefind command.

In my example below, I have a firewall with a comment saying Wireguard, but you can find it by any parameter or a combination of parameters.

Using .id:

/ipv6/firewall/filter> :put [print as-value where comment ~ "Wireguard"]
.id=*1B;action=accept;chain=input;comment=Wireguard;disabled=false;dst-port=13231-13235;dynamic=false;in-interface-list=WAN;invalid=false;log=false;log-prefix=;protocol=udp
/ipv6/firewall/filter> enable *1B

Using find:

/ipv6/firewall/filter> enable [find where comment ~"Wireguard"]

Also, if check if the mqtt client remains connected if you have issues, but it seems from your message that it is connected.

Thanks I really appreciate your comment and will look to implement it this way, the problem I have forgetting the firewall logic, it is even when I just subscribe to the topic and print the data to the MikroTik log this stops working after a few mins, it’s like it has just lost the connection to there broker, I’ve found if I manually publish a message from the router to the broker it’s kicks back in to life.

Then maybe try setting auto-connect for you broker to yes, to try maintaining the connection. It’s currently set to no in your config above.

thank you that's a good spot I hadn't noticed that option, it not available in Winbox just the CLI