
Security News
Oxlint Introduces Type-Aware Linting Preview
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.
@dbc-tech/nest-dapr
Advanced tools
Develop NestJs microservices using Dapr pubsub and bindings.
Dapr Module for Nest built on top of the Dapr JS SDK.
npm i --save @dbc-tech/nest-dapr
Install Dapr as per getting started guide. Ensure Dapr is running with
dapr --version
Output:
CLI version: 1.7.1
Runtime version: 1.7.4
The following scaffolds a Nest project with the nest-dapr package and demonstrates using Nest with Dapr using RabbitMQ pubsub & queue bindings.
Install Nest CLI
npm install -g @nestjs/cli
Scaffold Nest project
nest new nest-dapr
cd nest-dapr/
Install nest-dapr package
npm i --save @dbc-tech/nest-dapr
Import DaprModule
in AppModule
class
@Module({
imports: [DaprModule.register()],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
Import DaprClient
from @dapr/dapr
package and add dependency to AppController
class
import { DaprClient } from '@dapr/dapr';
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(
private readonly appService: AppService,
private readonly daprClient: DaprClient,
) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}
Create pubsub & topic names used for pubsub operations and message interface
const pubSubName = 'my-pubsub';
const topicName = 'my-topic';
interface Message {
hello: string;
}
@Controller()
Create endpoint to publish topic message
@Post('pubsub')
async pubsub(): Promise<boolean> {
const message: Message = { hello: 'world' };
return this.daprClient.pubsub.publish(pubSubName, topicName, message);
}
Create pubsub handler which will subscribe to the topic and log the received message
@DaprPubSub(pubSubName, topicName)
pubSubHandler(message: Message): void {
console.log(`Received topic:${topicName} message:`, message);
}
Create Dapr pubsub component in components
folder
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: my-pubsub
namespace: default
spec:
type: pubsub.rabbitmq
version: v1
metadata:
- name: host
value: amqp://guest:guest@localhost:5674
Save file as components/rabbitmq-pubsub.yaml
Create docker-compose.yml
in the project root used to run RabbitMQ
version: '3.9'
services:
pubsub:
image: rabbitmq:3-management-alpine
ports:
- 5674:5672
- 15674:15672
Start RabbitMQ
docker-compose up
Create script to bootstrap your Nest project using Dapr sidecar. Update package.json
and add script
"scripts": {
..
"start:dapr": "dapr run --app-id nest-dapr --app-protocol http --app-port 50001 --dapr-http-port 50000 --components-path ./components npm run start"
},
Start Nest app with Dapr
npm run start:dapr
Invoke endpoint to publish the message
curl -X POST localhost:3000/pubsub
This should publish a message to RabbitMQ which should be consumed by the handler and written to the console:
== APP == Received topic:my-topic message: { hello: 'world' }
Full example
import { DaprClient } from '@dapr/dapr';
import { DaprPubSub } from '@dbc-tech/nest-dapr';
import { Controller, Get, Post } from '@nestjs/common';
import { AppService } from './app.service';
const pubSubName = 'my-pubsub';
const topicName = 'my-topic';
interface Message {
hello: string;
}
@Controller()
export class AppController {
constructor(
private readonly appService: AppService,
private readonly daprClient: DaprClient,
) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
@Post('pubsub')
async pubsub(): Promise<boolean> {
const message: Message = { hello: 'world' };
return this.daprClient.pubsub.publish(pubSubName, topicName, message);
}
@DaprPubSub(pubSubName, topicName)
pubSubHandler(message: Message): void {
console.log(`Received topic:${topicName} message:`, message);
}
}
FAQs
Develop NestJs microservices using Dapr pubsub and bindings
We found that @dbc-tech/nest-dapr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oxlint’s new preview brings type-aware linting powered by typescript-go, combining advanced TypeScript rules with native-speed performance.
Security News
A new site reviews software projects to reveal if they’re truly FOSS, making complex licensing and distribution models easy to understand.
Security News
Astral unveils pyx, a Python-native package registry in beta, designed to speed installs, enhance security, and integrate deeply with uv.