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
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
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
})
