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 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-azure-service-bus

v0.0.18

Published

A dynamic module for NestJS that provides integration with Azure Service Bus.

Downloads

1,495

Readme

NestJS Azure Service Bus

npm version license coverage

A dynamic module for NestJS that provides integration with Azure Service Bus.

Installation

npm install nestjs-azure-service-bus

Description

The NestJS Azure Service Bus package allows you to easily integrate Azure Service Bus into your NestJS applications. It provides decorators for injecting Azure Service Bus senders and receivers, as well as a dynamic module for configuring the Azure Service Bus client.

Usage

AzureServiceBusModule - Importing the module

To use the Azure Service Bus module, import it into your NestJS application's root module:

import { Module } from '@nestjs/common';
import { AzureServiceBusModule } from 'nestjs-azure-service-bus';

@Module({
  imports: [
    AzureServiceBusModule.forRoot({
      connectionString: '<your-connection-string>',
    }),
  ],
})
export class AppModule {}

Replace <your-connection-string> with your Azure Service Bus connection string.

AzureServiceBusModule - Injecting Senders and Receivers

You can use the Sender and Receiver decorators provided by the module to inject Azure Service Bus senders and receivers into your classes:

import { Injectable } from '@nestjs/common';
import { Sender, Receiver } from 'nestjs-azure-service-bus';

@Injectable()
export class MyService {
  constructor(
    @Sender('my-queue') private readonly sender: ServiceBusSender,
    @Receiver('my-queue') private readonly receiver: ServiceBusReceiver,
  ) {}

  // Use the sender and receiver in your methods
}

Replace 'my-queue' with the name of your Azure Service Bus queue.

AzureServiceBusModule - Configuration Options

The forRoot method of the AzureServiceBusModule accepts a configuration object with two possible options:

  • connectionString: The connection string for your Azure Service Bus namespace.
  • fullyQualifiedNamespace: The fully qualified namespace of your Azure Service Bus namespace.

You can provide either the connectionString or the fullyQualifiedNamespace, but not both.

AzureServiceBusModule - Dynamic Module Options

The forFeature method of the AzureServiceBusModule allows you to configure senders and receivers dynamically. It accepts an options object with two properties:

  • senders: An array of sender names.
  • receivers: An array of receiver names.
import { Module } from '@nestjs/common';
import { AzureServiceBusModule } from 'nestjs-azure-service-bus';

@Module({
  imports: [
    AzureServiceBusModule.forFeature({
      senders: ['queue-example'],
      receivers: ['queue-example'],
    }),
  ],
})
export class QueueModule {}

This will create senders and receivers for the specified queues.

import { ServiceBusSender } from '@azure/service-bus';
import { Injectable } from '@nestjs/common';
import { Sender } from 'nestjs-azure-service-bus';

@Injectable()
export class QueueSenderService {
  constructor(
    @Sender('test-queue') private readonly sender: ServiceBusSender,
  ) {}
  async sendMessage(body: string) {
    await this.sender.sendMessages({ body });
  }
}
import { ServiceBusReceiver } from '@azure/service-bus';
import { Injectable, OnModuleInit } from '@nestjs/common';
import { Receiver } from 'nestjs-azure-service-bus';

@Injectable()
export class QueueReceiverService implements OnModuleInit {
  constructor(
    @Receiver('test-queue') private readonly receiver: ServiceBusReceiver,
  ) {}
  onModuleInit() {
    this.receiver.subscribe({
      processMessage: async (message) => {
        console.log(`message.body: ${message.body}`);
      },
      processError: async (args) => {
        console.log(
          `Error occurred with ${args.entityPath} within ${args.fullyQualifiedNamespace}: `,
          args.error,
        );
      },
    });
  }
}
import { Module } from '@nestjs/common';
import { QueueSenderService } from './queue-sender.service';
import { AzureServiceBusModule } from 'nestjs-azure-service-bus';
import { QueueReceiverService } from './queue-receiver.service';

@Module({
  imports: [
    AzureServiceBusModule.forFeature({
      receivers: ['test-queue'],
      senders: ['test-queue'],
    }),
  ],
  providers: [QueueSenderService, QueueReceiverService],
  exports: [QueueSenderService],
})
export class QueueModule {}

for another method the ServiceBusReceiver and ServiceBusSender see the azure sdk

AzureServiceBusAdminModule - Importing the module

To use the Azure Service Bus Admin module, import it into your NestJS application's root module:

import { Module } from '@nestjs/common';
import { AzureServiceBusAdminModule } from 'nestjs-azure-service-bus';

@Module({
  imports: [
    AzureServiceBusAdminModule.forRoot({
      connectionString: '<your-connection-string>',
    }),
  ],
})
export class AppModule {}

Replace <your-connection-string> with your Azure Service Bus connection string.

AzureServiceBusAdminModule - Injecting Admin decorator

You can use the Admin decorator provided by the module to inject Azure Service Bus admin client into your classes:

import { Injectable } from '@nestjs/common';
import { Admin } from 'nestjs-azure-service-bus';

@Injectable()
export class MyService {
  constructor(
    @Admin() private readonly admin: ServiceBusAdministrationClient,
  ) {}

  async createQueue(queue: string){
    return this.admin.createQueue(queue)
  }
  async createTopic(topic: string){
    return this.admin.createTopic(topic)
  }
  async queueRuntimeProperties(queue: string){
    return this.admin.getQueueRuntimeProperties(queue)
  }
  async deleteQueue(){
    await this.admin.deleteQueue(queue)
  }
  //...
}

for another method the ServiceBusAdministrationClient see the azure sdk

Support

License

This package is MIT licensed.