How to test an MQTT broker in API Spector
MQTT is the publish and subscribe protocol behind a lot of IoT and messaging systems. API Spector now speaks it, so you can connect to a broker, subscribe to topics, and publish messages next to your HTTP and WebSocket requests.
Switch a request to MQTT
In the request's protocol switcher, alongside HTTP, WS, SOAP, gRPC, and IO, choose MQTT. The URL bar becomes the broker address. API Spector connects over TCP with mqtt:// and mqtts://, and over WebSocket with ws:// and wss://, for example mqtt://broker.example.com:1883.
Connect
Fill in the connection details below the URL: an optional Client ID, which is generated for you if you leave it empty, a Username and Password, and the Clean session flag. The password field understands {{variables}}, so you can reference a secret from your environment instead of typing it into the request. Pick a default QoS of 0, 1, or 2, then click Connect. The status dot turns green when the broker accepts the connection.
Subscribe to topics
Enter a topic or a wildcard filter in the Subscribe field, such as sensors/# or devices/+/status, and click Subscribe. Every message the broker delivers appears in the session log with its topic, its QoS, and a retain marker when the broker set one. Unsubscribe stops that topic without dropping the connection.
Publish messages
Set a Publish topic, type a payload, and click Publish, or press Ctrl+Enter. The retain checkbox and the QoS apply to what you send. Your published message shows in the same log, so when you are subscribed to the same topic you see the full round trip.
Turn it into a test
Watching the log tells you a message arrived, but it does not fail a build when it does not. For that, open the Test section below Publish. MQTT is streaming, so a test works as a collect then assert step: set a Collect window in milliseconds, optionally a Publish payload that is sent to your Publish topic, and write assertions in the sp.* sandbox. The messages that arrive during the window are handed to the script as sp.messages.
sp.test('a reading arrives', () => {
const m = sp.messages.find(m => m.topic === 'sensors/temp');
sp.expect(m).to.exist;
sp.expect(m.json().c).to.be.a('number');
});
Click Run test. API Spector opens a fresh connection, subscribes to your Subscribe topic, publishes if you set a payload, collects messages for the window, runs the script, and shows a pass or fail for each assertion along with how many messages it collected. Each message carries topic, qos, retain, timestamp, and json() / text() helpers.
Run it with the rest of your tests
An MQTT request that has a test script is a real test, not just a panel. It runs as a pass or fail step in the Runner and from the command line the same way an HTTP request does: connect, subscribe, publish, collect, assert. So a check like "the device reports its status within two seconds" can sit in a collection run and in CI next to your HTTP assertions.
Why it helps
Testing a broker used to mean a separate tool or a command line client, and confirming behavior meant reading messages by eye. With MQTT in API Spector the broker sits in the same workspace as your REST and realtime requests, with the same variables and the same session file, a publish and its echo show up side by side in one log, and a collect then assert test turns "the message looked right" into a result you can run and trust.