Nestjs Microservice RabbitMq

981 views Asked by At

I'm creating a microservice, where I need to receive messages from rabbit. I am facing the following error ERROR [Server] There is no matching event handler defined in the remote service. Event pattern: undefined

main.ts

import { NestFactory } from '@nestjs/core';
import { Transport, MicroserviceOptions } from '@nestjs/microservices';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    AppModule,
    {
      transport: Transport.RMQ,
      options: {
        urls: ['amqp://localhost:5672'],
        queue: 'my-queue',
        queueOptions: { durable: false },
      },
    },
  );
  app.listen();
}
bootstrap();

app.module.ts

import { Module } from '@nestjs/common';
import { RabbitMQConsumerService } from './rabbitmq-consumer.service';

@Module({
  imports: [],
  providers: [RabbitMQConsumerService],
})
export class AppModule {}

rabbitmq-consumer.service.ts

import { Injectable } from '@nestjs/common';
import {
  Ctx,
  MessagePattern,
  Payload,
  RmqContext,
} from '@nestjs/microservices';

@Injectable()
export class RabbitMQConsumerService {
  @MessagePattern('my-queue')
  async processMessage(@Payload() data: any, @Ctx() context: RmqContext) {
    console.log('Mensagem recebida:', data);
  }
}

when I send the message I expected to receive it in the nestjs service, but I get the error ERROR [Server] There is no matching event handler defined in the remote service. Event pattern: undefined

1

There are 1 answers

0
Binbo On

I think the publisher u are using is publishing to the right queue but with different or wrongly configured message pattern. The subscribing microservice received it but there is nor handler defined for the given message pattern (or undefined) hence that error occurred.

In case the publisher is also a nestJS microservice it should follow the send function arguments:

this.client.send(pattern, payload);

Where client is ClientProxy object.

https://docs.nestjs.com/microservices/basics#client