> For the complete documentation index, see [llms.txt](https://docs.nestjstools.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.nestjstools.com/broker-integration/azure-service-bus.md).

# Azure Service Bus

Absolutely! Here's a great starting structure for a **GitBook page** explaining your Azure Service Bus integration.

***

## &#x20;Azure Service Bus Integration

> This guide explains how to configure and use Azure Service Bus in your messaging-based application using `@nestjstools/messaging`.

***

### 📦 Installation

Make sure you’ve installed the following dependencies:

```bash
npm install @nestjstools/messaging @nestjstools/messaging-azure-service-bus-extension
or
yarn add @nestjstools/messaging @nestjstools/messaging-azure-service-bus-extension
```

***

### 🛠️ Channel Configuration

#### Basic Setup queue

```ts
channels: [
  new AzureServiceBusChannelConfig({
    name: 'azure-channel',
    connectionString: 'Endpoint=sb://your-namespace.servicebus.windows.net/;SharedAccessKeyName=...;',
    queue: 'your-queue-name',
    autoCreate: false, // Requires admin permission
    enableConsumer: true,
  }),
],
```

#### Topic/Subscription Example

```ts
channels: [
  new AzureServiceBusChannelConfig({
    name: 'azure-pubsub-channel',
    connectionString: 'Endpoint=sb://your-namespace.servicebus.windows.net/;SharedAccessKeyName=...;',
    topic: 'your-topic',
    subscription: 'your-subscription',
    mode: Mode.TOPIC,
    autoCreate: true,
    enableConsumer: true,
  }),
],
```

***

### ⚙️ Config Options

| Property           | Description                                                       | Default   |
| ------------------ | ----------------------------------------------------------------- | --------- |
| `name`             | Internal channel name                                             | —         |
| `connectionString` | Azure Service Bus connection string                               | —         |
| `queue`            | Queue name (required for `Mode.QUEUE`)                            | —         |
| `topic`            | Topic name (required for `Mode.TOPIC`)                            | —         |
| `subscription`     | Subscription name (required for `Mode.TOPIC`)                     | —         |
| `mode`             | `'queue'` or `'topic'`                                            | `'queue'` |
| `autoCreate`       | Auto-create queue/topic/subscription (requires admin permissions) | `false`   |
| `enableConsumer`   | Enable message receiving                                          | `true`    |

***

### 📤 Dispatching a Message

```ts
import { Controller, Get } from '@nestjs/common';
import { CreateUser } from './application/command/create-user';
import { IMessageBus, MessageBus, RoutingMessage } from '@nestjstools/messaging';

@Controller()
export class AppController {
  constructor(
    @MessageBus('azure.bus') private azureMessageBus: IMessageBus,
  ) {}

  @Get('/azure')
  createUser(): string {
    this.azureMessageBus.dispatch(
      new RoutingMessage(
        new CreateUser('John FROM Azure bus'),
        'my_app_command.create_user',
      ),
    );

    return 'Message sent';
  }
}
```

***

### ✅ Tips

* `autoCreate` works **only if** the channel has `enableConsumer = true` and the connection string has **management permissions**.
* Use `mode: Mode.TOPIC` only when using `topic` and `subscription`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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://docs.nestjstools.com/broker-integration/azure-service-bus.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.
