Create a new MQTT client.
Optional
options: ClientOptionsOptional client options.
Connects to an MQTT broker.
Broker URL or connection options.
Optional
options: ConnectOptionsOptional connection options.
Disconnects from the MQTT broker synchronously.
Optional
options: EndOptionsOptional disconnect options.
Disconnects from the MQTT broker asynchronously.
Optional
options: EndOptionsOptional disconnect options.
Promise that resolves when disconnection is complete.
Listen for the connect
event.
Callback for connect event.
Listen for the end
event.
Callback for end event.
Listen for the reconnect
event.
Callback for reconnect event.
Listen for incoming messages.
Callback for message event.
Listen for errors.
Callback for error event.
Publish a message to a MQTT topic synchronously.
The topic to publish to.
The message payload (string or ArrayBuffer).
Optional
options: PublishOptionsOptional publish options.
Publishes a message to an MQTT topic asynchronously.
The topic to publish to.
The message payload (string or ArrayBuffer).
Optional
options: PublishOptionsOptional publish options.
Promise that resolves when publish is complete.
Attempts to reconnect to the MQTT broker. Uses the same connection parameters as the last successful connection.
Subscribe to one or more topics synchronously.
Topic(s) or subscription options.
Optional
options: SubscribeOptionsOptional subscription options.
Subscribe to one or more topics asynchronously.
Topic(s) or subscription options.
Optional
options: SubscribeOptionsOptional subscription options.
Promise that resolves when subscription is complete.
Unsubscribe from one or more topics synchronously.
Topic(s) to unsubscribe from.
Optional
options: UnsubscribeOptionsOptional unsubscribe options.
Unsubscribe from one or more topics asynchronously.
Topic(s) to unsubscribe from.
Optional
options: UnsubscribeOptionsOptional unsubscribe options.
Promise that resolves when unsubscription is complete.
MQTT client for connecting to brokers and managing MQTT operations.
The
Client
class provides a high-level, event-driven interface for interacting with MQTT brokers. It supports both synchronous and asynchronous operations for connecting, subscribing, publishing, and unsubscribing, and is designed to be familiar to users of MQTT.js.Event-Driven Usage
Register event handlers for connection lifecycle and message events using
.on()
method:connect
message
end
reconnect
error
All event handlers are executed in the context of the k6 VU event loop.
SSL/TLS
xk6-mqtt does not provide its own custom TLS configuration options. Instead, it relies on the standard k6 TLS configuration for all SSL/TLS settings. This means you should configure certificates, verification, and other TLS-related options using the same environment variables and configuration files as you would for any other k6 protocol. This approach ensures consistency across your k6 tests and leverages the robust, well-documented TLS support already present in k6.
Supported Broker URL Schemas
xk6-mqtt supports connecting to MQTT brokers using the following URL schemas:
mqtt://
mqtts://
tcp://
mqtt://
, plain TCP connectionssl://
mqtts://
, secure SSL/TLS connectiontls://
mqtts://
, secure SSL/TLS connectionws://
wss://
If you omit the schema in the broker URL,
mqtt://
(plain TCP) is used as the default.Example: Basic Usage