---
url: https://node-ts.github.io/bus/api/bus-core/classes/BusConfiguration.md
---
[API reference](../../index.md) / [@node-ts/bus-core](../index.md) / BusConfiguration

# Class: BusConfiguration

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:62](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L62)

## Constructors

### Constructor

```ts
new BusConfiguration(): BusConfiguration;
```

#### Returns

`BusConfiguration`

## Methods

### asSendOnly()

```ts
asSendOnly(): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:162](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L162)

Configure the bus to only send messages and not receive them. No queues or subscriptions will be created for
this service.

#### Returns

`this`

#### Throws

BusAlreadyInitialized if called after the bus has been built

***

### build()

```ts
build(): BusInstance;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:91](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L91)

Constructs an instance of a bus from the configuration

#### Returns

[`BusInstance`](BusInstance.md)

#### Throws

BusAlreadyInitialized if the bus has already been built

#### Throws

ContainerNotRegistered if a class handler's constructor takes arguments and no container is registered

#### Throws

TransportAlreadyInUse if the transport is already used by another bus

#### Throws

MessageTypesConflict if the message types passed to `withMessageTypes()` define a `$name` or type
differently

#### Throws

MessageTypeReferenceNotFound if message types passed to `withMessageTypes()` refer to a type they
don't define

***

### withConcurrency()

```ts
withConcurrency(concurrency): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:356](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L356)

Sets the message handling concurrency beyond the default value of 1, which will increase the number of messages
handled in parallel.

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `concurrency` | `number` |

#### Returns

`this`

#### Default

```ts
1
```

#### Throws

BusAlreadyInitialized if called after the bus has been built

***

### withContainer()

```ts
withContainer(container): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:377](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L377)

Use a local dependency injection/IoC container to resolve handlers
and workflows.

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `container` | [`ContainerAdapter`](../interfaces/ContainerAdapter.md) | An adapter to an existing DI container to fetch class instances from |

#### Returns

`this`

#### Throws

BusAlreadyInitialized if called after the bus has been built

***

### withCustomHandler()

```ts
withCustomHandler<MessageType>(messageHandler, customResolver): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:226](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L226)

Registers a custom handler that receives messages from external systems, or messages that don't implement the
Message interface from @node-ts/bus-messages

#### Type Parameters

| Type Parameter |
| ------ |
| `MessageType` |

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `messageHandler` | [`HandlerDefinition`](../type-aliases/HandlerDefinition.md)<`MessageType`> | A handler that receives the custom message |
| `customResolver` | [`CustomResolver`](../interfaces/CustomResolver.md)<`MessageType`> | A discriminator that determines if an incoming message should be mapped to this handler. |

#### Returns

`this`

#### Throws

BusAlreadyInitialized if called after the bus has been built

***

### withHandler()

#### Call Signature

```ts
withHandler(...classHandler): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:180](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L180)

Register a handler for a specific message type. When Bus is initialized it will configure
the transport to subscribe to this type of message and upon receipt will forward the message
through to the provided message handler

##### Parameters

| Parameter | Type |
| ------ | ------ |
| ...`classHandler` | [`ClassConstructor`](../type-aliases/ClassConstructor.md)<[`Handler`](../interfaces/Handler.md)<[`MessageBase`](../type-aliases/MessageBase.md), [`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md)<[`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md), [`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md)>>>\[] |

##### Returns

`this`

##### Throws

BusAlreadyInitialized if called after the bus has been built

#### Call Signature

```ts
withHandler<MessageType, TMessageAttributes>(...functionHandler): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:181](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L181)

Register a handler for a specific message type. When Bus is initialized it will configure
the transport to subscribe to this type of message and upon receipt will forward the message
through to the provided message handler

##### Type Parameters

| Type Parameter | Default type |
| ------ | ------ |
| `MessageType` *extends* [`Message`](../../bus-messages/classes/Message.md) | - |
| `TMessageAttributes` *extends* [`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md)<[`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md), [`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md)> | [`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md)<[`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md), [`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md)> |

##### Parameters

| Parameter | Type |
| ------ | ------ |
| ...`functionHandler` | `object`\[] |

##### Returns

`this`

##### Throws

BusAlreadyInitialized if called after the bus has been built

***

### withInterruptSignals()

```ts
withInterruptSignals(signals): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:432](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L432)

Sets the process signals that gracefully stop the bus, replacing the defaults. Pass an empty array to listen
for none, so a host such as NestJS or AWS Lambda can own shutdown and call `bus.stop()` or `bus.dispose()` itself.
Send-only buses never listen for signals.

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `signals` | `Signals`\[] | The signals that stop the bus |

#### Returns

`this`

#### Default

```ts
['SIGINT', 'SIGTERM']
```

#### Throws

BusAlreadyInitialized if called after the bus has been built

#### Examples

```ts
// Let the host handle SIGINT and SIGTERM
Bus.configure().withInterruptSignals([])
```

```ts
// Also stop on SIGUSR2
Bus.configure().withInterruptSignals(['SIGINT', 'SIGTERM', 'SIGUSR2'])
```

***

### withLogger()

```ts
withLogger(loggerFactory): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:282](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L282)

Configures Bus to use a different logging provider than the default console logger

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `loggerFactory` | [`LoggerFactory`](../type-aliases/LoggerFactory.md) |

#### Returns

`this`

#### Throws

BusAlreadyInitialized if called after the bus has been built

***

### withMessageReadMiddleware()

```ts
withMessageReadMiddleware<TransportMessageType>(messageReadMiddleware): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:392](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L392)

Register optional middlewares that will run for each message that is polled from the transport
Note these middlewares only run when polling successfully pulls a message off the Transports queue
After all the user defined middlewares have registered.

#### Type Parameters

| Type Parameter | Default type |
| ------ | ------ |
| `TransportMessageType` | `unknown` |

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `messageReadMiddleware` | [`Middleware`](../type-aliases/Middleware.md)<[`TransportMessage`](../interfaces/TransportMessage.md)<`TransportMessageType`>> |

#### Returns

`this`

#### Throws

BusAlreadyInitialized if called after the bus has been built

***

### withMessageTypes()

```ts
withMessageTypes(...messageTypes): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:327](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L327)

Gives the bus the message types generated by `bus generate-message-types` (from `@node-ts/bus-cli`), which
say how to restore the Dates, Maps, Sets, bigints and classes of each message and workflow state it
receives. Pass the `messageTypes` export of every generated file whose messages the bus handles. Calling
this again adds to the message types already passed.

A bus that receives messages must have message types for every message it handles and every workflow state
it persists, or `initialize()` throws `MessageTypesMissing`. Send-only buses don't need them.

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| ...`messageTypes` | [`MessageTypes`](../../bus-messages/interfaces/MessageTypes.md)\[] | the message types of one or more generated files |

#### Returns

`this`

#### Throws

BusAlreadyInitialized if called after the bus has been built

#### Example

```ts
import { messageTypes as orderMessageTypes } from '@my-org/order-messages'
import { messageTypes } from './message-types.generated'

const bus = Bus.configure()
  .withMessageTypes(orderMessageTypes, messageTypes)
  .withHandler(orderPlacedHandler)
  .build()
```

***

### withPersistence()

```ts
withPersistence(persistence): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:341](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L341)

Configures Bus to use a different persistence provider than the default InMemoryPersistence provider.
This is used to persist workflow data and is unused if not using workflows.

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `persistence` | [`Persistence`](../interfaces/Persistence.md) |

#### Returns

`this`

#### Throws

BusAlreadyInitialized if called after the bus has been built

***

### withReceiver()

```ts
withReceiver(receiver): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:453](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L453)

Register a receiving mechanism that will be used to receive messages and deliver
them to the dispatcher.

Usually the bus will connect to a transport and receive messages directly. However
a different receiver plugin can be used that will become responsible for this instead.

Once the bus is configured, messages can be received by passing the received message into bus.receive().

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `receiver` | | [`Receiver`](../interfaces/Receiver.md)<`unknown`, [`TransportMessage`](../interfaces/TransportMessage.md)<`unknown`>, `unknown`> | `undefined` | The receiver mechanism to use, or `undefined` to use the default behaviour |

#### Returns

`this`

#### Throws

BusAlreadyInitialized if called after the bus has been built

***

### withRetryStrategy()

```ts
withRetryStrategy(retryStrategy): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:409](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L409)

Configure @node-ts/bus to use a different retry strategy that determines delays between
retrying failed messages.

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `retryStrategy` | [`RetryStrategy`](../interfaces/RetryStrategy.md) |

#### Returns

`this`

#### Default

```ts
DefaultRetryStrategy
```

#### Throws

BusAlreadyInitialized if called after the bus has been built

***

### withSerializer()

```ts
withSerializer(serializer): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:299](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L299)

Configures Bus to use a different serialization provider. The provider is responsible for
transforming messages to/from a serialized representation, as well as ensuring all object
properties are a strong type. The bus passes its message types (see `withMessageTypes()`) to the
serializer each time it restores an object, so one serializer can be shared by several buses.

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `serializer` | [`Serializer`](../interfaces/Serializer.md) |

#### Returns

`this`

#### Default

```ts
JsonSerializer
```

#### Throws

BusAlreadyInitialized if called after the bus has been built

***

### withTransport()

```ts
withTransport(transportConfiguration): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:269](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L269)

Configures Bus to use a different transport than the default MemoryQueue. A transport instance holds one
queue and one connection, so each bus needs its own instance.

#### Parameters

| Parameter | Type |
| ------ | ------ |
| `transportConfiguration` | [`Transport`](../interfaces/Transport.md) |

#### Returns

`this`

#### Throws

BusAlreadyInitialized if called after the bus has been built

***

### withWorkflow()

```ts
withWorkflow(...workflow): this;
```

Defined in: [packages/bus-core/src/service-bus/bus-configuration.ts:248](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/service-bus/bus-configuration.ts#L248)

Register a workflow definition so that all of the messages it depends on will be subscribed to
and forwarded to the handlers inside the workflow

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| ...`workflow` | ( | [`FunctionWorkflow`](../interfaces/FunctionWorkflow.md)<[`WorkflowState`](WorkflowState.md)> | [`ClassConstructor`](../type-aliases/ClassConstructor.md)<[`Workflow`](Workflow.md)<[`WorkflowState`](WorkflowState.md)>>)\[] | Classes that extend `Workflow`, or workflows declared with `defineWorkflow` |

#### Returns

`this`

#### Throws

BusAlreadyInitialized if called after the bus has been built

#### Throws

WorkflowNotRecognized if a workflow is neither a class that extends `Workflow` nor declared with
`defineWorkflow`

#### Example

```ts
Bus.configure().withWorkflow(OrderWorkflow, defineWorkflow(ShippingState).startedBy(OrderPaid, ...))
```
