Skip to content

MqttClientOptions

reference
1 min readUpdated

Kind: Interface

Source: packages/microservices/external/mqtt-options.interface.ts

Part of: 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

PropertyType
portnumber
hoststring
hostnamestring
pathstring
protocol`'wss'
wsOptions{ [x: string]: any; }
keepalivenumber
clientIdstring
protocolIdstring
protocolVersionnumber
cleanboolean
reconnectPeriodnumber
connectTimeoutnumber
usernamestring
passwordstring
incomingStoreany
outgoingStoreany
queueQoSZeroboolean
properties{ sessionExpiryInterval?: number; receiveMaximum?: number; maximumPacketSize?: number; topicAliasMaximum?: number; requestResponseInformation?: boolean; requestProblemInformation?: boolean; userProperties?: object; authenticationMethod?: string; authenticationData?: any; }
reschedulePingsboolean
serversArray<{ host: string; port: number; }>
resubscribeboolean
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 mqtt-options.interface.ts:136-150

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

Was this page helpful?

Download as PDF
MqttClientOptions — NestJS head-to-head