Skip to content

MqttRecordSerializer

reference
1 min readUpdated

Kind: Class

Source: packages/microservices/serializers/mqtt-record.serializer.ts

Part of: Microservices

MqttRecordSerializer converts an MqttRecord into a JSON string suitable for MQTT transport. It preserves MQTT publish options such as qos, retain, and dup, while serializing object payloads into the record's data field.

Implements: Serializer

Methods

MethodSignatureReturns
serializeserialize(packet: ReadPacket)string

Diagram

mermaid
graph LR
  A[MqttRecord] --> B[MqttRecordSerializer.serialize]
  B --> C[Serialize object data when needed]
  C --> D[Merge MQTT publish options]
  D --> E[JSON string payload]
  E --> F[MQTT transport]

Usage

ts
import { MqttRecord } from '@nestjs/microservices';
import { MqttRecordSerializer } from '@nestjs/microservices/serializers/mqtt-record.serializer';

const record = new MqttRecord(
  { deviceId: 'sensor-42', temperature: 21.5 },
  {
    qos: 1,
    retain: true,
  },
);

const serializer = new MqttRecordSerializer();
const payload = serializer.serialize(record);

console.log(payload);
// {"data":"{\"deviceId\":\"sensor-42\",\"temperature\":21.5}","qos":1,"retain":true}

AI Coding Instructions

  • Pass an MqttRecord containing both the message payload (data) and MQTT-specific publish options.
  • Object payloads are JSON-stringified before being embedded in the final serialized message; avoid passing circular object structures.
  • Keep MQTT options compatible with the underlying MQTT client, including fields such as qos, retain, and dup.
  • Use this serializer at the MQTT transport boundary; application handlers should typically work with deserialized message data instead of the serialized string.

Relationships

  • IMPORTS → isObject

Was this page helpful?

Download as PDF
MqttRecordSerializer — NestJS head-to-head