Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/k6/latest/javascript-api/k6-x-mqtt/client/on.md, or by sending Accept: text/markdown to https://grafana.com/docs/k6/latest/javascript-api/k6-x-mqtt/client/on/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
Client.on()
The on() method registers event handlers for various MQTT client lifecycle and message events. All event handlers execute in the context of the k6 VU event loop.
Signature
client.on(event, listener)Parameters
| Parameter | Type | Description |
|---|---|---|
| event | string | Event name (connect, message, end, reconnect, error) |
| listener | function | Callback function to handle the event |
Events
connect
Triggered when the client successfully connects to the broker.
Signature
client.on("connect", () => {
// Connection established
})message
Triggered when a message is received on a subscribed topic.
Signature
client.on("message", (topic, payload) => {
// Handle received message
})Parameters
| Parameter | Type | Description |
|---|---|---|
| topic | string | The topic the message was received on |
| payload | ArrayBuffer | The message payload as binary data |
end
Triggered when the client disconnects from the broker.
Signature
client.on("end", () => {
// Connection closed
})reconnect
Triggered when the client attempts to reconnect to the broker.
Signature
client.on("reconnect", () => {
// Reconnection attempt
})error
Triggered when an error occurs during any MQTT operation.
Signature
client.on("error", (error) => {
// Handle error
})Parameters
| Parameter | Type | Description |
|---|---|---|
| error | object | Error object |
| error.name | string | Always “MQTTError” |
| error.message | string | Error description |
| error.method | string | The method where the error occurred |
Was this page helpful?
Related resources from Grafana Labs

