Menu
Choose a product
Viewing: v1.7.x (latest)
Find another version
Scroll for more
Open source
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
JavaScript
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
JavaScript
client.on("connect", () => {
// Connection established
})message
Triggered when a message is received on a subscribed topic.
Signature
JavaScript
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
JavaScript
client.on("end", () => {
// Connection closed
})reconnect
Triggered when the client attempts to reconnect to the broker.
Signature
JavaScript
client.on("reconnect", () => {
// Reconnection attempt
})error
Triggered when an error occurs during any MQTT operation.
Signature
JavaScript
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
Additional helpful documentation, links, and articles:
Video

Performance testing and observability in Grafana Cloud
Optimize user experiences with Grafana Cloud. Learn real-time insights, performance testing with k6, and continuous validation with Synthetic Monitoring.
Events

User-centered observability: load testing, real user monitoring, and synthetics
Learn how to use load testing, synthetic monitoring, and real user monitoring (RUM) to understand end users' experience of your apps. Watch on demand.