npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@madeweb/nestjs-service-bus

v1.0.0

Published

NestJS Azure Service Bus Microservice Transport

Downloads

37

Readme

Description

Azure Service Bus custom transport for NestJS microservices. Supports queues and topics/subscriptions with proper message routing via NestJS patterns.

Installation

$ npm i --save @azure/service-bus @madeweb/nestjs-service-bus

Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | connectionString | string | — | Azure Service Bus connection string | | queueName | string | — | Queue name (required when not using topics) | | topicName | string | — | Topic name (required when not using queues) | | subscriptionName | string | — | Subscription name (required with topics) | | receiveMode | 'peekLock' \| 'receiveAndDelete' | 'peekLock' | Message receive mode | | clientOptions | ServiceBusClientOptions | — | Azure SDK client options (retry, webSocket, userAgent) |

Client options (clientOptions)

Pass advanced options to the underlying ServiceBusClient:

interface ServiceBusClientOptions {
  retryOptions?: RetryOptions;
  webSocketOptions?: WebSocketOptions;
  userAgentOptions?: UserAgentOptions;
}

Usage

Microservice server (consumer)

// main.ts
import { NestFactory } from '@nestjs/core';
import { MicroserviceOptions } from '@nestjs/microservices';
import { AzureServiceBusStrategy } from '@madeweb/nestjs-service-bus';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, {
    strategy: new AzureServiceBusStrategy({
      connectionString: 'Endpoint=sb://<namespace>.servicebus.windows.net/;SharedAccessKeyName=<key>;SharedAccessKey=<key>',
      queueName: 'my-queue',
    }),
  });

  await app.listen();
}
bootstrap();

Handling messages

The pattern sent by the client is stored in the message's applicationProperties and used to route to the correct handler:

import { Controller } from '@nestjs/common';
import { MessagePattern, Payload } from '@nestjs/microservices';

@Controller()
export class MessageController {
  @MessagePattern('order.created')
  async handleOrderCreated(@Payload() data: any) {
    console.log('Order created:', data);
  }

  @MessagePattern('order.cancelled')
  async handleOrderCancelled(@Payload() data: any) {
    console.log('Order cancelled:', data);
  }
}

Client (producer)

import { Injectable } from '@nestjs/common';
import { AzureServiceBusClient } from '@madeweb/nestjs-service-bus';
import { firstValueFrom } from 'rxjs';

@Injectable()
export class OrderService {
  private readonly client: AzureServiceBusClient;

  constructor() {
    this.client = new AzureServiceBusClient({
      connectionString: process.env.AZURE_SERVICE_BUS_CONNECTION!,
      queueName: 'my-queue',
    });
  }

  async createOrder(data: any) {
    await this.client.connect();
    return firstValueFrom(this.client.emit('order.created', data));
  }
}

Using Topics / Subscriptions

// Server
new AzureServiceBusStrategy({
  connectionString: process.env.AZURE_SERVICE_BUS_CONNECTION!,
  topicName: 'orders',
  subscriptionName: 'service-a',
  receiveMode: 'peekLock',
  clientOptions: {
    retryOptions: { maxRetries: 3 },
  },
});

// Client
new AzureServiceBusClient({
  connectionString: process.env.AZURE_SERVICE_BUS_CONNECTION!,
  topicName: 'orders',
});

Hybrid app (HTTP + microservice)

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.connectMicroservice({
    strategy: new AzureServiceBusStrategy({
      connectionString: process.env.AZURE_SERVICE_BUS_CONNECTION!,
      queueName: process.env.AZURE_SERVICE_BUS_QUEUE!,
    }),
  });

  await app.startAllMicroservices();
  await app.listen(3000);
}

Message routing

Messages are routed using the pattern field stored in the Azure Service Bus message applicationProperties. This allows a single queue/topic to handle multiple message patterns with different @MessagePattern() handlers — just like NestJS built-in transports (RabbitMQ, Redis, etc.).

Error handling

  • PeekLock mode: messages are automatically completed on success and abandoned on error
  • ReceiveAndDelete mode: messages are removed from the broker on delivery (no settlement required)
  • Server errors are logged via NestJS Logger instead of console.error

Security and contributions

License

NestJS Azure Service Bus is MIT licensed.