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.

The following example creates a handler for a hotel booking application

Declare a command that models the ReserveRoom command

// 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.

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.

Register the handler with the BusConfiguration.

Last updated

Was this helpful?