...
Regular Expressions are a broad topic and cannot be covered in full here. We This guide will demonstrate the use of HiveMQ’s pattern matching capabilities by creating a Trace Recording, matching all clients with a clientId beginning with publisher0
, followed by a single character. Further the special case of creating a recording for topic tree patterns with use of the +
character will be shown.
Info |
---|
The key to utilising RegEx in Trace Recording filters is knowing that client identifiers and topics are seen as regular strings by the filters, with no MQTT specific treatment of tokens |
Prerequisites
In order to verify, that our messages are being delivered as we expect we will subscribe to a test topic pattern.
In our example we will capture messages by clients including their identifiers in the topic structure. This is a common pattern in production environments.
HiveMQ’s MQTT CLI lets us do subscribe to all of these this in one line like socommand by using the wildcard operator:
Code Block |
---|
mqtt sub -t 'our/+/topic' -h localhost -i subscriber00 |
To have an easier time differentiating our clients, we gave this one the clientId identifier subscriber00
.
Let this session remain active throughout the test.
...
First, choose a new, unique name. This need not match a topic name and only serves to help identify the recording we are about to create.
Client Filter
Next, we have to define a Client Filter. Here we will make use of the wildcard character .
that acts as a quantifier for “any single character”.
...
After clicking Add Topic Client Filter
it should become visible as an existing filter:
...
Filtering for topics using the wildcard operator
In order for our recording to match the subscription of subscriber00
, which uses MQTT’s single level wildcard character, a Regular Expression will allow us to do so. Here foreslashes need to be escaped with \
.
Code Block |
---|
our\/.+\/topic |
Info |
---|
The |
Message Type
From here we can select the types of messages we are interested in. For this example, we are only interested in PUBLISH messages.
...
Code Block |
---|
for i in `seq 5 15`; do sleep 1; mqtt pub -t '"our/publisher0$i/topic'" -h 127.0.0.1 -m "message by publisher0$i" -i publisher0$i; done |
Once $i
reaches 10, the clientID will contain a character more than we specified in our filter. We expect to not to see any messages from publisher010
to publisher015
in our recording.
...
Code Block |
---|
2020-04-09 15:52:43,526 - [publisher05] - Received PUBLISH message (topic: our/publisher05/topic, QoS: 0, retain: false, duplicate delivery: false) 2020-04-09 15:52:45,297 - [publisher06] - Received PUBLISH message (topic: our/publisher06/topic, QoS: 0, retain: false, duplicate delivery: false) [...] 2020-04-09 15:53:16,852 - [publisher09] - Received PUBLISH message (topic: our/publisher09/topic, QoS: 0, retain: false, duplicate delivery: false) |
...