Events
An event is a message emitted by the system when "something" happens. Again this could be a technical task being completed such as a DatabaseBackedUp, LoadBalancerScaledOut or as a result of changes in your business CreditCardCharged, UserRegistered, PackageShipped.
Creating an Event
Events are class definitions that extend from Event, eg:
import { Event } from '@node-ts/bus-messages'
export class CreditCardCharged extends Event {
/**
* A unique name that identifies the message. This should be done in namespace style syntax,
* ie: organisation/domain/event-name
*/
$name = 'my-app/accounts/credit-card-charged'
/**
* The contract version of this message. This can be incremented if this message changes the
* number of properties etc to maintain backwards compatibility
*/
$version = 1
/**
* A credit card was successfully charged
* @param creditCardToken Identifies the card that was charged
* @param amount The amount, in USD, that the card was charged for
*/
constructor (
readonly creditCardToken: string,
readonly amount: number
) {
}
}Publishing an Event
Events can have 0-to-many different subscribers, who are generally interested in performing a next action as a result of the event being raised.
Use .publish() to publish an event:
Handling an Event
Events get processed by a Handler. This is a function or a class function that receives the message as a parameter and performs an operation. When the handler returns the message is deleted from the queue.
Implementing a function based handler
Implementing a class based handler
Register the handler with the bus configuration
Remember to .start() the bus to start handling messages
Last updated
Was this helpful?