Skip to content

Lifecycle hooks ​

A bus emits events as it sends, publishes, receives and handles messages. This page lists them and shows how to listen for them, for logging, metrics or auditing.

Each hook is an emitter on the bus. on() adds a listener and returns a function that removes it. once() listens for the next event only, and off() removes a listener.

ts
const unsubscribe = bus.onError.on(({ message, error, attributes }) =>
  console.error('Failed to handle message', {
    messageName: message.$name,
    correlationId: attributes?.correlationId,
    error
  })
)

// Later, to stop listening
unsubscribe()

Listeners are called synchronously and aren't awaited. If one returns a promise that rejects, the bus logs it.

Hooks ​

HookEmittedWith
beforeSendbefore a command is sent to the transportcommand, attributes
afterSendafter the transport has sent a commandcommand, attributes
beforePublishbefore an event is published to the transportevent, attributes
afterPublishafter the transport has published an eventevent, attributes
afterReceiveafter a message is read from the queue, before middlewaremessage, the transport's message
beforeDispatchbefore a message is dispatched to its handlersmessage, attributes, handlers
afterDispatchafter every handler of a message has succeededmessage, attributes
onErrorwhen reading, dispatching or handling a message failsmessage, error, attributes, rawMessage

Messages sent from inside a handler are held until it resolves, so beforeSend and beforePublish fire when the handler sends them, and afterSend and afterPublish once they've been sent.

ts
bus.beforeSend.on(({ command, attributes }) =>
  console.debug('Sending', command.$name, attributes)
)
bus.afterSend.on(({ command }) => console.debug('Sent', command.$name))
bus.beforePublish.on(({ event, attributes }) =>
  console.debug('Publishing', event.$name, attributes)
)
bus.afterPublish.on(({ event }) => console.debug('Published', event.$name))
bus.afterReceive.on(({ message }) => console.debug('Received', message.id))
bus.beforeDispatch.on(({ message, handlers }) =>
  console.debug('Dispatching', message.$name, handlers.length)
)
bus.afterDispatch.on(({ message }) => console.debug('Handled', message.$name))
bus.onError.on(({ message, error, rawMessage }) =>
  console.debug('Failed', message.$name, rawMessage?.id, error)
)

See also ​

Released under the MIT License.