1.0.31 • Published 4 years ago

nest-rabbitmq-microservices v1.0.31

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

Nest rabbitmq microservices

Description

This module is based in @nestjs-plus/rabbitmq to facilitate the usage of event and rpc patterns between rabbitmq and nestjs.

Usage

Init the module in your nest app.

// app.module.ts
import { Module } from '@nestjs/common';
import { RabbitMicroservices } from 'nest-rabbitmq-microservices';
const appName = "my-app"
@Module({
  imports: [
    RabbitMicroservices.forRootAsync({
      useFactory: (config: ConfigService) => {
        return {
          exchanges: [
            {
              name: `rpc.${appName}`, // For RPC comunications
              type: 'topic',
            },
            {
              name: `event.${appName}`, // For events
              type: 'topic',
            },
          ],
          uri: "amqp://localhost:5672",
        };
      },
      imports: [ConfigModule.forRoot()],
      inject: [ConfigService],
    }),

  ],
 providers: [
    AppController, // All controllers have to be imported as service
  ],
})
export class AppModule {}

Define your controller with events or RPC methods by using the Rpc or Subscribe decorators where Rpc can be combined with EmmitEvents for automatically produce events of the method.

import { Injectable } from '@nestjs/common';
import { BrokerService, Rpc, Message, EmmitEvents, Subscribe } from 'nest-rabbitmq-microservices';

const appName = "my-app"
const emmiterApp = "my-app" // because of same app
@Injectable()
export class AppController {
  constructor(
    private readonly broker: BrokerService, // This always has to be imported
  ) {}

  @Rpc(`${appName}.create-cat`)
  @EmmitEvents(appName, 'cat-created', 'cat-creation-failed')
  createCat({
    data,
		correlationId, // If you need them
		eventId,
    causationId
  }: Message<any>): Promise<SignInResponseDto> {
	   return { cat : data.cat }
  }

	@Subcribe(`${appName}.${emmiterApp}.cat-created`)
  sayCat({ data }:Message<any>){
		console.log(data)
	}

}

Now how to try it ?

You can try it by using BrokerService included in the package.

import { Get } from '@nestjs/common';
import { BrokerService } from 'nest-rabbitmq-microservices';


@Controller()
export class AppController {
  constructor(private readonly broker: BrokerService) {}

  @Get('/create-cat')
  login(@Body() cat): Promise<any> {
    return this.broker.send({ name : "michi" });
  }
}
1.0.29

4 years ago

1.0.28

4 years ago

1.0.31

4 years ago

1.0.30

4 years ago

1.0.27

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.25-test

4 years ago

1.0.24-test

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago