> For the complete documentation index, see [llms.txt](https://bus.node-ts.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bus.node-ts.com/installing/handling-messages.md).

# Handling messages

At some point you'll want to handle messages that have been sent to your application. This can be done by creating a handler and registering it with the [BusConfiguration](/reference/busconfiguration.md).

The following example creates a handler for a hotel booking application&#x20;

Declare a command that models the **ReserveRoom** command

```typescript
// reserve-room.ts

import { Command } from '@node-ts/bus-messages'

export class ReserveRoom extends Command {
  $name = 'reservations/reserve-room'
  $version = 0
  
  constructor (
    readonly roomId: string,
    readonly bookingId: string
  ) {
    super()
  }
}
```

Create a handler that receives a command to `ReserveRoom` that it delegates the operation to a `reservationService`.&#x20;

{% hint style="info" %}
Handlers should be kept as dumb as possible and delegate the work to dedicated services. This will help keep the messaging concerns of your application decoupled from the actual work it needs to do.
{% endhint %}

```typescript
// reserve-room-handler.ts

import { handlerFor } from '@node-ts/bus-core'
import { ReserveRoom } from '../messages'
import { reservationService } from '../services'

export const reserveRoomHandler = handlerFor(
  ReserveRoom,
  command => reservationService.reserveRoom(command.reservation, command.bookingId)
)
```

Register the handler with the BusConfiguration.

```typescript
// application.ts

import { Bus, BusInstance } from '@node-ts/bus-core'
import { reserveRoomHandler } from './handlers'
import { ReserveRoom } from './messages'

let bus: BusInstance
const start = async () => {
  bus = await Bus.configure()
    .withHandler(reserveRoomHandler)
    .initialize()
    
  // Start the bus to commence processing messages
  await bus.start()
}

start
  .then(async () => bus.send(new ReserveRoom(
    '63a65cf0-d239-4b83-96da-f33f013db23a',
    '12b85a56-e929-47a8-9ac3-e87739d5d215'
  ))
  .catch(console.error)
```


---

# 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, and the optional `goal` query parameter:

```
GET https://bus.node-ts.com/installing/handling-messages.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
