# Lifecycle hooks

**@node-ts/bus** exposes a number of lifecycle hooks that can be subscribed to. These are **EventEmitter** instances that follow the standard node `.on()` and `.off()` notation.

### Hooks

**beforeSend**

called just before a command is sent to the underlying transport

```
bus.beforeSend.on(({ command, attributes }) => {})
```

**beforePublish**

called just before a event is published to the underlying transport

```
bus.beforePublish.on(({ event, attributes }) => {})
```

**afterReceive**&#x20;

called after a message has been read from the queue, and before it is dispatched to handlers

```
bus.afterReceive.on(transportMessage => {})
```

**beforeDispatch**

called before a message is dispatched to its handlers

```
bus.beforeDispatch.on(({ message, attributes, handlers }) => {})
```

**afterDispatch**

called after a message has been successfully handled and the message deleted from the transport

```
bus.afterDispatch.on(({ message, attributes }) => {})
```

**onError**

called when an error occurred reading/dispatching/handling a message

```
bus.onError.on(({ message, error, attributes, rawMessage }) => {})
```


---

# Agent Instructions: 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:

```
GET https://bus.node-ts.com/guide/lifecycle-hooks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
