Search and contents

Playing captured MQTT messages

A small command that publishes a file of captured MQTT messages to a broker, one after another, at the pace you choose. It is for standing up a stand-in for a plant's broker while you build or test a config, so the MQTT System Connector (see mqtt-system-connector.md) has something to read before the real plant is reachable.

This is test tooling, not part of the public API. The options, the file format and the exit codes may change in any release, and the stability policy (public-api-stability.md) does not cover them.

It connects the way a Kindred connector does: TLS is verified in full and a password is only ever sent over TLS. How a broker is set up to be reached is in mqtt-broker.md.

Setup and the commands in this document

Every command below runs as written once this block has been run in the same shell. It makes a message file, a profile for a broker on this machine's standard port, and a profile for a port where nothing listens. The commands run from the repository root with the project's Python environment active, and the first one publishes to a broker on localhost:1883.

WORK=$(mktemp -d)
cat > "$WORK/messages.jsonl" <<'JSON'
{"topic": "kindred-player-demo/line1/state", "payload": "RUN", "retain": true}
{"topic": "kindred-player-demo/line1/count", "payload": {"count": 1}}
{"topic": "kindred-player-demo/line1/count", "payload": {"count": 2}, "delay": 0.5}
JSON
echo '{"host": "localhost", "port": 1883}' > "$WORK/broker.json"
echo '{"host": "localhost", "port": 1}' > "$WORK/nobroker.json"
echo '{"topic": "kindred-player-demo/x"}' > "$WORK/bad.jsonl"

The message file

JSON lines: one message per line, in the order they are sent. A JSON array of the same objects is read too.

KeyMeaning
topic Required. One concrete topic; a wildcard is refused.
payload A string is sent as its text. Any other JSON value is sent as compact JSON.
payload_base64 The payload as base64, for bytes that are not text. Exactly one of payload and payload_base64.
delay Seconds to wait before this message. Default 0, at most 3600.
retain true to have the broker hold the message. Default false. A retained message with an empty payload clears the one held.
qos 0, 1 or 2. Default 0.

tst and mid are ignored, so the output of mosquitto_sub -F '%J' plays as it is (it records no delays, so the messages go out one straight after another). Any other key is an error. The whole file is checked before anything is sent, and an error names the message's number, never its content. A file may hold at most 10 000 messages and 10 MB.

Playing a file

python -m engine.mqtt_player_cli "$WORK/messages.jsonl" --broker "$WORK/broker.json"

The recorded delays are kept. --speed FACTOR scales them: 2 is twice as fast, 0.5 half as fast, and the factor is above 0 and at most 1000. --rate N ignores the recorded delays and sends N messages a second (above 0, at most 1000), the first at once. The two cannot be combined.

python -m engine.mqtt_player_cli "$WORK/messages.jsonl" --broker "$WORK/broker.json" --speed 10

--retain has the broker hold every message, whether or not its line says so. Messages held this way stay on the broker until something clears them, so use a topic prefix you can find again.

Describing the broker

--broker names a JSON file with the same keys as a connector's broker section: host and port, or socket_path for a Unix socket; ca_bundle_path to turn TLS on; certificate_path with private_key_path for a client certificate; username and password; client_id is replaced by a fresh one on every run. The port defaults to 1883, or 8883 with a CA bundle.

There is no option for a password or a key, so none ever appears on a command line or in the shell's history. The password goes in the profile file or in the environment variable KINDRED_PLAYER_PASSWORD, which wins. A profile file that holds a password is refused unless only its owner can read it (chmod 600). Nothing the command prints includes a credential.

Exit codes

CodeMeaning
0 Every message was published.
1 A message was not confirmed by the broker (at QoS 1 or 2, within 10 seconds); the run stopped there, and that message may still arrive, so do not assume it is missing.
2 The file, the profile or the options could not be used; nothing was sent.
3 The broker could not be reached, or refused the connection.
4 Stopped by SIGINT or SIGTERM before the end. The player disconnects at once, even in the middle of a delay or while connecting; at QoS 1 or 2 it first finishes waiting (up to 10 seconds) for the message in flight.

A file with a bad message sends nothing:

python -m engine.mqtt_player_cli "$WORK/bad.jsonl" --broker "$WORK/broker.json"

A broker that cannot be reached is a different code:

python -m engine.mqtt_player_cli "$WORK/messages.jsonl" --broker "$WORK/nobroker.json" --connect-timeout 2

--connect-timeout SECONDS bounds the wait for the broker (default 5), which must be above 0 and at most 600. A broker that refuses the first attempt, or is not listening yet, is reported at once rather than retried.

The module behind the command

python -m engine.mqtt_player_cli is a thin wrapper over engine/mqtt_player.py, which a test can call directly:

  • read_messages(path) reads and validates the whole file into Message records (topic, payload bytes, delay, retain, qos). It raises PlayerInputError on a bad file, naming the message's number and never its content.

  • read_profile(path) does the same for the broker profile (PlantBrokerProfile, at most 64 KiB), with the password from KINDRED_PLAYER_PASSWORD winning.

  • delays(messages, speed=, rate=) works out the wait before each message.

  • play(connection, messages, waits, ...) publishes in order over a BrokerConnection and returns a PlayResult:

    • published and total count the messages sent and in the file.
    • failed_at is the 1-based number of the message the broker did not take.
    • interrupted says whether the run was stopped early.

    It stops at the first message not confirmed within publish_timeout (10 seconds) or when its stop event is set.

  • monotonic_wait(stop) is the default pacing. It keeps the schedule against the clock, so a slow publish shortens the next wait instead of being made up in a burst.

The module is test tooling and carries the same no-stability notice as the command.

Limits

The player publishes; it does not record. mosquitto_sub -F '%J' captures a file it can play. It does not decode Sparkplug B, but a Sparkplug payload captured as bytes plays through payload_base64.

Release: v0.11.0