@dbc-tech/nest-dapr

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dbc-tech/nest-dapr

Develop NestJs microservices using Dapr pubsub and bindings

0.0.12
Version published
Maintainers
1
Created

Develop NestJs microservices using Dapr pubsub and bindings.

Description

Dapr Module for Nest built on top of the Dapr JS SDK.

Installation

npm i --save @dbc-tech/nest-dapr

Requirements

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

Quick start

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

Package last updated on 03 Jul 2022

Did you know?

Socket

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.

Install

Related posts