Test Expired Messages
This article is to explain how to generate an “expired” message and how to catch it in the $expired/# topic.
Instructions
Prepare a HiveMQ broker with a TCP listener on localhost:1883.
On the broker, enable Expired Messages Topic add-on.
Enabling Expired Messages Topic add-on has 2 workings:The Expired Messages Topic add-on prefaces the topic of expired messages with
$expired/.
So, if a message published tomy/topic/testis expired, it will be published to topic$expired/my/topic/test.The Expired Messages Topic add-on exposes additional metrics in the HiveMQ standard metrics:
com.hivemq.messages.expired-messages
com.hivemq.messages.expired.topic.expired.count
and other metrics.
Following steps explain how to create a situation when a message is expired.
On the localhost, install MQTT CLI command line client.
Start the broker.
To monitor for expired messages, create a client
sub-expiredwith subscription to$expired/#topic filter or a shared subscription$share/shared-expired/$expired:mqtt subscribe -i sub-expired -t '$expired/#' -J -dwhere:
-i 'sub-expired'– client identifier;-t '$expired/#'– topic filter;-J– output as JSON;-d– verbose output.
Create a client
sub-testwith subscription to some topictestwith a long session expiry and disconnect the client. To disconnect the command line client, terminate its terminal session by pressing Ctrl+C:mqtt subscribe -i sub-test -t test -se 6000 --no-cleanStartwhere:
-se 6000– session expiry 600 seconds;--no-cleanStart– persistent session;
In the brokers Control Center → Clients → Refresh View and make sure that the client
sub-testis present in the list, subscribed to the topictestand offline.Publish a message to the topic
testfrom MQTT v.5 client, with a short message expiry and QoS 1.mqtt publish -i pub-test -t test -m Hello -V 5 -e 2where:
-m Hello– message “Hello”;-V 5– MQTT v.5 client;-e 2– message expiry 2s;
Expected results:
After the message expiry interval (2s) the published message expires and
is published to the topic
$expired/testand received by thesub-expiredclient.metrics updated
com.hivemq.messages.expired-messages
com.hivemq.messages.expired.topic.expired.count
Script for quickly setting up a test environment with HiveMQ with Prometheus and Grafana monitoring: https://github.com/guinp1n/docker-hivemq-with-prometheus