Documentation Index
Fetch the curated documentation index at: https://grafana_com_website/llms.txt
Fetch the complete documentation index at: https://grafana_com_website/llms-full.txt
Use this file to discover all available pages before exploring further.
STOP! If you are an AI agent or LLM, read this before continuing. This is the HTML version of a Grafana documentation page. Always request the Markdown version instead - HTML wastes context. Get this page as Markdown: /docs/k6/latest/javascript-api/k6-x-mqtt/client/on.md (append .md) or send Accept: text/markdown to /docs/k6/latest/javascript-api/k6-x-mqtt/client/on/. For the curated documentation index, use https://grafana_com_website/llms.txt. For the complete documentation index, use https://grafana_com_website/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

