> For the complete documentation index, see [llms.txt](https://docs.nestjstools.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.nestjstools.com/getting-started/message-handler.md).

# Message Handler

## 💬 Defining a Message & Message Handler

Messages are the core units of communication in your system. Each message can have a dedicated handler that processes it when received. This section explains how to define a **message** and implement a **handler**.

***

### 📨 Define Your Message

A message is a simple class representing the data being sent between services or components:

```ts
// send-message.ts
export class SendMessage {
  constructor(
    public readonly content: string,
  ) {}
}
```

This message can now be published on a bus and handled by one or more consumers.

***

### 🛠️ Define a Message Handler

Handlers process incoming messages. They must implement the `IMessageHandler<T>` interface.

```ts
import { Injectable } from '@nestjs/common';
import { MessageHandler, IMessageHandler, MessageResponse } from '@nestjstools/messaging';
import { SendMessage } from './send-message';

@Injectable()
@MessageHandler('your.message') // This should match the message route you publish to
export class SendMessageHandler implements IMessageHandler<SendMessage> {
  async handle(message: SendMessage): Promise<MessageResponse | void> {
    console.log(message.content);
    // Add your business logic here
  }
}
```

***

### 🔁 Multiple Routes

{% hint style="info" %}
You can associate a single handler with **multiple message routes**:

```ts
@MessageHandler('your.message', 'your.message2')
```

{% endhint %}

***

### Optional: Use `@DenormalizeMessage()` Decorator

If you want NestJS to automatically instantiate the incoming message as a proper `class` (rather than just receiving raw JSON), use the `@DenormalizeMessage()` decorator:

```ts
async handle(@DenormalizeMessage() message: SendMessage): Promise<MessageResponse | void> {
  // message is a real SendMessage instance
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.nestjstools.com/getting-started/message-handler.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
