# RmqRecordSerializer

**Kind:** Class

**Source:** [`packages/microservices/serializers/rmq-record.serializer.ts`](https://github.com/nestjs/nest/blob/master/packages/microservices/serializers/rmq-record.serializer.ts#L6)

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

`RmqRecordSerializer` converts outgoing NestJS microservice packets into RabbitMQ-compatible record payloads. It preserves the request packet shape while extracting or merging RabbitMQ-specific metadata, such as headers and message properties, for transport delivery.

**Implements:** `Serializer`

## Methods

| Method | Signature | Returns |
|---|---|---|
| `serialize` | `serialize(packet: ReadPacket)` | `ReadPacket & Partial<RmqRecord>` |

## Diagram

```mermaid
graph LR
  A[Application Message] --> B[RmqRecordSerializer]
  B --> C[ReadPacket]
  B --> D[Partial RmqRecord]
  C --> E[RabbitMQ Transport]
  D --> E
```

## Usage

```ts
import { RmqRecordSerializer } from '@nestjs/microservices';

const serializer = new RmqRecordSerializer();

const packet = serializer.serialize({
  pattern: 'orders.created',
  data: {
    orderId: 'order_123',
    customerId: 'customer_456',
  },
});

client.send('orders.created', packet.data);
```

## AI Coding Instructions

- Use `RmqRecordSerializer` at the RabbitMQ transport boundary when outgoing packets may include RabbitMQ record metadata.
- Preserve the standard NestJS `ReadPacket` fields, especially `pattern` and `data`, when extending serialization behavior.
- Ensure RabbitMQ-specific options, such as headers or properties, are carried through without mutating the original payload.
- Keep custom serializers compatible with the `serialize(): ReadPacket & Partial<RmqRecord>` return contract.
- Test serialization with both plain message payloads and `RmqRecord`-style payloads containing transport metadata.

## How it works

- `RmqRecordSerializer` is an exported serializer class implementing `Serializer<ReadPacket, ReadPacket & Partial<RmqRecord>>`. Its `serialize` method accepts a packet with `pattern` and `data` fields. [rmq-record.serializer.ts:6-10](packages/microservices/serializers/rmq-record.serializer.ts#L6-L10) [packet.interface.ts:5-8](packages/microservices/interfaces/packet.interface.ts#L5-L8) [serializer.interface.ts:10-12](packages/microservices/interfaces/serializer.interface.ts#L10-L12)

- It recognizes a packet only when `packet.data` is truthy, is a non-null JavaScript object, and is an actual `RmqRecord` instance according to `instanceof`. [rmq-record.serializer.ts:11-15](packages/microservices/serializers/rmq-record.serializer.ts#L11-L15) [`isObject` checks non-null values whose `typeof` is `object`.](packages/common/utils/shared.utils.ts#L4-L5)

- For such a packet, `serialize` returns a **new** packet object: it copies the packet’s existing enumerable properties, replaces `data` with `RmqRecord.data`, and writes `RmqRecord.options` to a top-level `options` field. [rmq-record.serializer.ts:16-21](packages/microservices/serializers/rmq-record.serializer.ts#L16-L21) `RmqRecord` stores its payload in readonly `data` and can store optional `options`. [rmq.record-builder.ts:25-29](packages/microservices/record-builders/rmq.record-builder.ts#L25-L29)

- If the payload does not meet that runtime check—including plain objects that merely resemble a record—the method returns the original packet reference unchanged. [rmq-record.serializer.ts:22-23](packages/microservices/serializers/rmq-record.serializer.ts#L22-L23) The serializer test checks reference identity for a non-`RmqRecord` payload. [rmq-record.serializer.spec.ts:31-37](packages/microservices/test/serializers/rmq-record.serializer.spec.ts#L31-L37)

- The method has no explicit error handling or thrown validation error in its implementation. [rmq-record.serializer.ts:10-24](packages/microservices/serializers/rmq-record.serializer.ts#L10-L24) It does not modify the input itself when it unwraps an `RmqRecord`; that branch constructs and returns an object literal. [rmq-record.serializer.ts:16-21](packages/microservices/serializers/rmq-record.serializer.ts#L16-L21)

- `ClientRMQ` and `ServerRMQ` select this class as their serializer when transport options do not specify a custom serializer. [client-rmq.ts:488-490](packages/microservices/client/client-rmq.ts#L488-L490) [server-rmq.ts:424-426](packages/microservices/server/server-rmq.ts#L424-L426)

- In the RMQ client send and event paths, the serialized packet’s top-level `options` is read and then deleted before the remaining packet is JSON-encoded; those options are spread into RabbitMQ send options, with headers merged separately. [client-rmq.ts:393-412](packages/microservices/client/client-rmq.ts#L393-L412) [client-rmq.ts:446-484](packages/microservices/client/client-rmq.ts#L446-L484) Therefore, when this serializer returns the input unchanged, that later deletion operates on the caller’s packet object. [rmq-record.serializer.ts:22-23](packages/microservices/serializers/rmq-record.serializer.ts#L22-L23) [client-rmq.ts:447-451](packages/microservices/client/client-rmq.ts#L447-L451)

## Relationships

- IMPORTS → `isObject`
