# MqttClientOptions

**Kind:** Interface

**Source:** [`packages/microservices/external/mqtt-options.interface.ts`](https://github.com/nestjs/nest/blob/master/packages/microservices/external/mqtt-options.interface.ts#L8)

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

`MqttClientOptions` defines connection and protocol settings for an MQTT client used by the microservices transport layer. It supports direct MQTT/TCP connections as well as WebSocket-based MQTT transports, including client identity, keepalive behavior, and WebSocket-specific options.

## Properties

| Property | Type |
|---|---|
| `port` | `number` |
| `host` | `string` |
| `hostname` | `string` |
| `path` | `string` |
| `protocol` | `'wss' | 'ws' | 'mqtt' | 'mqtts' | 'tcp' | 'ssl' | 'wx' | 'wxs'` |
| `wsOptions` | `{ [x: string]: any; }` |
| `keepalive` | `number` |
| `clientId` | `string` |
| `protocolId` | `string` |
| `protocolVersion` | `number` |
| `clean` | `boolean` |
| `reconnectPeriod` | `number` |
| `connectTimeout` | `number` |
| `username` | `string` |
| `password` | `string` |
| `incomingStore` | `any` |
| `outgoingStore` | `any` |
| `queueQoSZero` | `boolean` |
| `properties` | `{ sessionExpiryInterval?: number; receiveMaximum?: number; maximumPacketSize?: number; topicAliasMaximum?: number; requestResponseInformation?: boolean; requestProblemInformation?: boolean; userProperties?: object; authenticationMethod?: string; authenticationData?: any; }` |
| `reschedulePings` | `boolean` |
| `servers` | `Array<{ host: string; port: number; }>` |
| `resubscribe` | `boolean` |
| `will` | `{ topic: string; payload: string; qos: QoS; retain: boolean; }` |
| `transformWsUrl` | `(url: string, options: any, client: any) => string` |

## Diagram

```mermaid
graph LR
  Client[Microservice MQTT Client] --> Options[MqttClientOptions]
  Options --> Connection[host / hostname / port / path]
  Options --> Transport[protocol]
  Options --> Identity[clientId]
  Options --> Protocol[protocolId / protocolVersion]
  Options --> Keepalive[keepalive]
  Options --> WebSocket[wsOptions]

  Transport --> MQTT[mqtt / mqtts / tcp / ssl]
  Transport --> WS[ws / wss / wx / wxs]
  WebSocket --> WS
```

## Usage

```ts
import type { MqttClientOptions } from './mqtt-options.interface';

const mqttOptions: MqttClientOptions = {
  host: 'broker.example.com',
  hostname: 'broker.example.com',
  port: 8883,
  path: '/mqtt',
  protocol: 'mqtts',
  clientId: 'orders-service',
  protocolId: 'MQTT',
  protocolVersion: 4,
  keepalive: 60,
  wsOptions: {},
};

// Pass mqttOptions to the MQTT client or microservice transport configuration.
```

## AI Coding Instructions

- Provide a valid `protocol` value that matches the target transport, such as `mqtts` for secure TCP or `wss` for secure WebSockets.
- Keep `host`/`hostname`, `port`, and `path` consistent with the MQTT broker endpoint; `path` is especially important for WebSocket connections.
- Use a stable, unique `clientId` per running service instance to avoid broker-side client session conflicts.
- Set `protocolId` and `protocolVersion` to values supported by the broker; common MQTT settings are `MQTT` and version `4`.
- Only populate `wsOptions` when using a WebSocket-based protocol and pass broker-required headers, TLS options, or proxy configuration there.

## How it works

`MqttClientOptions` is a TypeScript interface that describes MQTT connection options. It extends `ISecureClientOptions`, so its shape includes optional TLS key, certificate, CA, and `rejectUnauthorized` fields. [mqtt-options.interface.ts:8](packages/microservices/external/mqtt-options.interface.ts#L8) [mqtt-options.interface.ts:136-150](packages/microservices/external/mqtt-options.interface.ts#L136-L150)

All of its fields are optional. The declaration itself contains no runtime validation, defaults assignment, error throwing, or side effects. [mqtt-options.interface.ts:8-135](packages/microservices/external/mqtt-options.interface.ts#L8-L135)
