This is documentation for the next version of Grafana k6 documentation. For the latest stable release, go to the latest version.
Client
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.
Constructor
new Client(options)Creates a new MQTT client instance.
Parameters
Properties
QoS
Quality of Service enumeration for message delivery guarantees:
Methods
Example
Basic Usage
import { Client } from "k6/x/mqtt";
export default function () {
const client = new Client()
client.on("connect", () => {
console.log("Connected to MQTT broker")
client.subscribe("greeting")
client.publish("greeting", "Hello MQTT!")
})
client.on("message", (topic, message) => {
const str = String.fromCharCode.apply(null, new Uint8Array(message))
console.info("topic:", topic, "message:", str)
client.end()
})
client.on("end", () => {
console.log("Disconnected from MQTT broker")
})
client.connect(__ENV["MQTT_BROKER_ADDRESS"] || "mqtt://broker.emqx.io:1883")
}
