1.0.21 • Published 2 years ago

nestjs-rabbitmq-client v1.0.21

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years 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

2 years ago

1.0.18

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago