---
title: "Client.on( event, listener ) | Grafana k6 documentation"
description: "Register event handlers for MQTT client events"
---

# 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 ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
client.on(event, listener)
```

### Parameters

Expand table

| 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 ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
client.on("connect", () => {
  // Connection established
})
```

### message

Triggered when a message is received on a subscribed topic.

#### Signature

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
client.on("message", (topic, payload) => {
  // Handle received message
})
```

#### Parameters

Expand table

| 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 ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
client.on("end", () => {
  // Connection closed
})
```

### reconnect

Triggered when the client attempts to reconnect to the broker.

#### Signature

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
client.on("reconnect", () => {
  // Reconnection attempt
})
```

### error

Triggered when an error occurs during any MQTT operation.

#### Signature

JavaScript ![Copy code to clipboard](/media/images/icons/icon-copy-small-2.svg) Copy

```javascript
client.on("error", (error) => {
  // Handle error
})
```

#### Parameters

Expand table

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