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

# Interface: BusSender

Defined in: [packages/bus-core/src/handler/bus-sender.ts:17](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/handler/bus-sender.ts#L17)

Sends commands and publishes events. `BusInstance` implements it, and so does the `HandlerContext` that's
passed to every handler, so code that only needs to send messages can depend on this interface and be given
either one, or a plain object in a test.

## Example

```ts
const placeOrder = async (sender: BusSender, orderId: string) =>
  sender.send(new PlaceOrder(orderId))

// In production
await placeOrder(bus, '1')
// In a test
const sent: Command[] = []
await placeOrder({ send: async c => { sent.push(c) }, publish: async () => {} }, '1')
```

## Extended by

* [`HandlerContext`](HandlerContext.md)

## Methods

### publish()

```ts
publish<TEvent>(event, messageAttributes?): Promise<void>;
```

Defined in: [packages/bus-core/src/handler/bus-sender.ts:37](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/handler/bus-sender.ts#L37)

Publishes an event to the transport. Inside a handler, the event is buffered and only published once the
handler resolves, and is dropped if the handler fails.

#### Type Parameters

| Type Parameter |
| ------ |
| `TEvent` *extends* [`Event`](../../bus-messages/classes/Event.md) |

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `event` | `TEvent` | The event to publish |
| `messageAttributes?` | `Partial`<[`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md)<[`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md), [`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md)>> | Attributes to attach to the outgoing message. The `correlationId` and `stickyAttributes` of the message being handled are added when published from a handler. |

#### Returns

`Promise`<`void`>

***

### send()

```ts
send<TCommand>(command, messageAttributes?): Promise<void>;
```

Defined in: [packages/bus-core/src/handler/bus-sender.ts:25](https://github.com/node-ts/bus/blob/2e6cf65c27833b707ae31ee623cfb6343770f434/packages/bus-core/src/handler/bus-sender.ts#L25)

Sends a command to the transport. Inside a handler, the command is buffered and only sent once the handler
resolves, and is dropped if the handler fails.

#### Type Parameters

| Type Parameter |
| ------ |
| `TCommand` *extends* [`Command`](../../bus-messages/classes/Command.md) |

#### Parameters

| Parameter | Type | Description |
| ------ | ------ | ------ |
| `command` | `TCommand` | The command to send |
| `messageAttributes?` | `Partial`<[`MessageAttributes`](../../bus-messages/interfaces/MessageAttributes.md)<[`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md), [`MessageAttributeMap`](../../bus-messages/interfaces/MessageAttributeMap.md)>> | Attributes to attach to the outgoing message. The `correlationId` and `stickyAttributes` of the message being handled are added when sent from a handler. |

#### Returns

`Promise`<`void`>
