# MqttRecordBuilder

**Kind:** Class

**Source:** [`packages/microservices/record-builders/mqtt.record-builder.ts`](https://github.com/nestjs/nest/blob/master/packages/microservices/record-builders/mqtt.record-builder.ts#L45)

**Part of:** [Microservices](subsystem-packages-microservices)

`MqttRecordBuilder` constructs `MqttRecord` instances for MQTT message handling within the microservices transport layer. It provides a fluent API for configuring payload data, QoS, retain and duplicate flags, and MQTT v5 properties before creating an immutable record with `build()`.

## Methods

| Method | Signature | Returns |
|---|---|---|
| `setData` | `setData(data: TData)` | `this` |
| `setQoS` | `setQoS(qos: MqttRecordOptions['qos'])` | `this` |
| `setRetain` | `setRetain(retain: MqttRecordOptions['retain'])` | `this` |
| `setDup` | `setDup(dup: MqttRecordOptions['dup'])` | `this` |
| `setProperties` | `setProperties(properties: MqttRecordOptions['properties'])` | `this` |
| `build` | `build()` | `MqttRecord` |

## Diagram

```mermaid
graph LR
  A[Message Payload] --> B[MqttRecordBuilder]
  C[QoS / Retain / Dup Flags] --> B
  D[MQTT Properties] --> B
  B -->|build()| E[MqttRecord]
  E --> F[MQTT Transport / Broker]
```

## Usage

```ts
import { MqttRecordBuilder } from './mqtt.record-builder';

const record = new MqttRecordBuilder()
  .setData(Buffer.from(JSON.stringify({ event: 'order.created', orderId: '123' })))
  .setQoS(1)
  .setRetain(false)
  .setDup(false)
  .setProperties({
    contentType: 'application/json',
  })
  .build();

// Pass the record to the MQTT publishing or transport layer.
```

## AI Coding Instructions

- Use the fluent setter pattern: configure all required values through `setData()`, `setQoS()`, and optional MQTT flags before calling `build()`.
- Validate payload format and QoS values at the integration boundary; MQTT QoS must match the broker and client capabilities.
- Use `setRetain(true)` only for messages intended to be stored by the broker and delivered to future subscribers.
- Set `dup` only when handling MQTT redelivery behavior; it should not be used as a general retry indicator.
- Keep MQTT v5 metadata in `setProperties()` and ensure property names and values are compatible with the MQTT client implementation.

## Used by

1 reference from 1 file. Each is a place in this repository where the symbol is actually used — go read one rather than trusting an example.

### Imported by (1)

- `MqttController` — `integration/microservices/src/mqtt/mqtt.controller.ts`:16
