nestjs-azure-service-bus-transporter
v1.0.1
Published
NestJS Azure Service Bus Microservice Transport
Downloads
643
Maintainers
Readme
Description
Azure Service Bus is a fully managed enterprise message broker with message queues and publish-subscribe topics (in a namespace). Service Bus is used to decouple applications and services from each other, providing the following benefits:
- Load-balancing work across competing workers
- Safely routing and transferring data and control across service and application boundaries
- Coordinating transactional work that requires a high-degree of reliability
Installation
To start building Azure Service Bus-based microservices, first install the required packages:
$ npm i nestjs-azure-service-bus-transporter
Server
To use the Azure Service Bus strategy, pass the following options object to the createMicroservice()
method:
// main.ts
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule,
{
strategy: new AzureServiceBusServer({
connectionString:
"Endpoint=sb://<Name>.servicebus.windows.net/;SharedAccessKeyName=<SharedAccessKeyName>;SharedAccessKey=<SharedAccessKey>",
options: {},
}),
}
);
To use in a hybrid application, pass the following options object to the connectMicroservice()
method:
// main.ts
const app = await NestFactory.create(AppModule);
await app.connectMicroservice({
strategy: new AzureServiceBusServer({
connectionString:
"Endpoint=sb://<Name>.servicebus.windows.net/;SharedAccessKeyName=<SharedAccessKeyName>;SharedAccessKey=<SharedAccessKey>",
options: {},
}),
});
await app.startAllMicroservices();
await app.listen(3000);
Options
The Azure Service Bus strategy exposes the properties described below.
Queue Consumer
To access the original Azure Service Bus message use the Queue
decorator as follows:
import { Queue } from 'nestjs-azure-service-bus-transporter';
import { ServiceBusReceiver } from '@azure/service-bus';
import { Payload, Ctx } from '@nestjs/microservices';
@Queue({
queueName: 'sample-topic',
receiveMode: 'peekLock', // or receiveAndDelete
options:{
autoCompleteMessages:true,
}
})
getMessages(
@Payload() message: ServiceBusMessage,@Ctx() context:AzureServiceBusContext) {
const serviceBusReceiver:ServiceBusReceiver= context.getArgs()[0];
console.log(message);
}
Options
Topic Consumer
To access the original Azure Service Bus message use the Subscription
decorator as follows:
import { Topic } from 'nestjs-azure-service-bus-transporter';
import { Payload, Ctx } from '@nestjs/microservices';
import { ServiceBusReceiver } from '@azure/service-bus';
@Topic({
topic: 'sample-topic',
subscription: 'sample-subscription',
receiveMode: 'peekLock', // or receiveAndDelete
options:{
autoCompleteMessages:true,
}
})
getMessages(@Payload() message: ServiceBusMessage) {
const serviceBusReceiver: ServiceBusReceiver = context.getArgs()[0];
console.log(message);
}
Options
Client
// app.module.ts
@Module({
imports: [
AzureServiceBusModule.forRoot([
{
name: 'SB_CLIENT',
connectionString: 'Endpoint=sb://<Name>.servicebus.windows.net/;SharedAccessKeyName=<SharedAccessKeyName>;SharedAccessKey=<SharedAccessKey>',
options: {},
},
]),
]
...
})
// or
@Module({
imports: [
AzureServiceBusModule.forRootAsync([
{
name: 'SB_CLIENT',
useFactory: (configService: ConfigService) => ({
connectionString: configService.get('connectionString'),
options: {}
}),
inject: [ConfigService],
},
]),
]
...
})
Since AzureServiceBusModule is a global module you can inject Clients into other modules providers and even controllers.
//example.service.ts
//provider
@Injectable()
export class exampleService {
constructor(
@Inject("SB_CLIENT") private readonly sbClient: AzureServiceBusClientProxy
) {}
}
//example.controller.ts
//controller
@Controller("example")
export class exampleController {
constructor(
@Inject("SB_CLIENT") private readonly sbClient: AzureServiceBusClientProxy
) {}
}
Producer
Event-based
const pattern = {
name: "sample-topic", // topic/queue name
options: {},
};
const data = {
body: "Example message",
};
this.sbClient.send(pattern, data).subscribe((response) => {
console.log(response); // reply message
});
Message-based
const pattern = {
name: "sample-topic", // topic/queue name
options: {},
};
const data = {
body: "Example message",
};
this.sbClient.emit(pattern, data);
Stay in touch
- Author - Santiagomrn
- Author - Niurmiguel
License
Nestjs Azure Service Bus is MIT licensed.