---
url: https://node-ts.github.io/bus/api/bus-core/interfaces/Handler.md
---
[API reference](../../index.md) / [@node-ts/bus-core](../index.md) / Handler

# Interface: Handler\<TMessage, TMessageAttributes>

Defined in: [packages/bus-core/src/handler/handler.ts:20](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/handler/handler.ts#L20)

Implemented by a class to indicate it acts as a handler for a given message

## Type Parameters

| Type Parameter | Default type |
| ------ | ------ |
| `TMessage` *extends* [`MessageBase`](../type-aliases/MessageBase.md) | [`MessageBase`](../type-aliases/MessageBase.md) |
| `TMessageAttributes` *extends* [`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md) | [`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md) |

## Properties

| Property | Type | Description | Defined in |
| ------ | ------ | ------ | ------ |
| <a id="property-messagetype"></a> `messageType` | [`MessageDeclaration`](../../bus-messages/type-aliases/MessageDeclaration.md)<`TMessage` & [`Message`](../../bus-messages/classes/Message.md)> | The type of message the class handles: a message class, or a definition from `defineCommand` or `defineEvent`. Define it as a getter (`get messageType() { return MyEvent }`) so that `withHandler()` can read it without constructing the handler. A class field is only readable from an instance, so the handler is constructed without its dependencies when it's registered. | [packages/bus-core/src/handler/handler.ts:30](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/handler/handler.ts#L30) |

## Methods

### handle()

```ts
handle(
   message, 
   attributes, 
   context
): unknown;
```

Defined in: [packages/bus-core/src/handler/handler.ts:39](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/handler/handler.ts#L39)

A function that is called each time a message of `messageType` is received

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `message` | `TMessage` | The message read from the bus |
| `attributes` | `TMessageAttributes` | Attributes of the message read from the bus |
| `context` | [`HandlerContext`](HandlerContext.md) | Sends, publishes, fails or returns messages through the bus that received the message |

#### Returns

`unknown`

Anything. A returned promise is awaited, and the value it resolves to is ignored
