1.0.21 • Published 10 months ago

nestjs-rabbitmq-client v1.0.21

Weekly downloads
-
License
MIT
Repository
-
Last release
10 months ago

RabbitMq Client | NestJs

Exemplo de uso:

Params

  • name - atribuído ao nome queue e do service (ex: AUTH_QUEUE/AUTH_SERVICE);

Variáveis (add no .env)

  • RABBITMQ_USER=admin
  • RABBITMQ_PASSWORD=admin
  • RABBITMQ_HOST=localhost:5672

Variáveis (QUEUE)

OBS: Você define apenas o nome entre RABBITMQ_NOMEDAQUEUE_QUEUE

Ex: Importação no main.ts

import { RabbitMqService } from 'nestjs-rabbitmq-client';

const rqmService = app.get<RabbitMqService>(RabbitMqService);

app.connectMicroservice<MicroserviceOptions>(
  rmq.getOptions(
    'AUTH', // name queue
  ),
);

OBS: Registro da QUEUE com base no app configurado (ex final: AUTH_QUEUE)

Ex: Modelo da variável de ambiente em seu projeto

RABBITMQ_AUTH_QUEUE=auth_queue

Env relacionada ao app que foi configurado.

OBS: Só precisar realizar a configuração acima e setar a env caso o app venha receber comunicação.

__

DISPARO

Ex: Importação no seumodule.module.ts

imports: [
  RabbitMqModule.register({
    name: 'PAYMENT', // name service
  }),
]

OBS: Utilizado para registrar o módulo/service e conectar-se na queue que será disparado as mensagens.

RABBITMQ_PAYMENT_QUEUE=payment-queue

Env relacionada ao app no qual a mensagem será enviada.

@Controller()
export class AppController {
  constructor(
    private readonly appService: AppService,
    @Inject('PAYMENT_SERVICE') private readonly paymentClient: ClientProxy,
  ) {}

  @Post('testing')
  async testingRmq() {
    const response = await firstValueFrom(
      this.paymentClient.send('testing.rmq', {
        message: 'Está é uma mensagem de teste :)',
      }),
    );

    console.log('response', response);
  }
}

__

RECEBIMENTO

Ex: Importação no controller Modo de uso utilizando ack

  • ack (padrão: false);
@Module({
  imports: [RabbitMqModule]
})

OBS: Exemplo para configurar no módulo que irá receber as mensagens.

RABBITMQ_PAYMENT_QUEUE=payment-queue

Env relacionada ao app no qual a mensagem será recebida.

import { RabbitMqService, RmqContextClient } from 'nestjs-rabbitmq-client';

@Controller('auth')

export class AuthController {
  constructor(
    private readonly rmqService: RabbitMqService,
  ){}


  @MessagePattern('testing.rmq')
  async testing(
    @Payload() payload: { message: string },
    @Ctx() context: RmqContextClient,
  ) {
    console.log('payload:testing.rmq', payload);
    this.rqmService.ack(context);
    return {
      message: `Ok. Mensagem recebida: ${payload.message}`,
    };
  }
}

Desenvolvido por Maycon | 2023

1.0.19

10 months ago

1.0.18

10 months ago

1.0.17

10 months ago

1.0.16

10 months ago

1.0.21

10 months ago

1.0.20

10 months ago

1.0.15

10 months ago

1.0.14

10 months ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago