Communicating with NestJs microservice from a Nodejs microservice with RabbitMq

Viewed 932

I am trying to call specific functions from a NestJs Microservice using RabbitMq but I am unable to hit the particular message patterns using nodejs.

The message pattern I am trying to access from my controller file

@MessagePattern('signin')
  async signin(
    @Payload(ValidationPipe) signInCredentialsDto: SignInCredentialsDto,
    @Ctx() context: RmqContext,
  ): Promise<{ accessToken: string }> {
    this.logger.verbose('Signing in ' + JSON.stringify(signInCredentialsDto));
    // const result = await this.authService.userSignin(signInCredentialsDto);
    // const channel = context.getChannelRef();
    // const originalMsg = context.getMessage();

    // channel.ack(originalMsg);
    return {
      accessToken: 'works',
    };
  }

My NestJs rabbitmq connection details

async function bootstrap() {
  const logger = new Logger('Main');
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    AppModule,
    {
      transport: Transport.RMQ,
      options: {
        urls: ['amqp://localhost:5672'],
        queue: 'rebus_queue',
        queueOptions: {
          durable: false,
        },
      },
    },
  );
  app.listen(() => {
    logger.log('Auth Microservice Started Successfully');
  });
}
bootstrap();

My nodejs client code:

amqp.connect("amqp://localhost:5672", (error0, connection) => {
  if (error0) throw error0;

  connection.createChannel((error1, channel) => {
    if (error1) throw error1;

    channel.assertQueue("rebus_queue", {
      durable: false,
    });

    app.get("/", (req: express.Request, res: express.Response) => {});

    app.post(
      "/auth/signin",
      async (req: express.Request, res: express.Response) => {
        channel.sendToQueue(
          "rebus_queue",
          Buffer.from(JSON.stringify(req.body))
        );
      }
    );
  });
});

When I hit the /auth/signin route I successfully hit the nestjs microservice however I don't know how to access the signin messagepattern.

As things stand, hitting the nodejs http endpoint causes the following error in the nestjs terminal

[Server] There is no matching event handler defined in the remote service. Event pattern: undefined. +50026ms
1 Answers

I had a typescrit/express publisher and a nestJS subscriber; I figured this worked properly.

import amqplib from 'amqplib';

const payload = Buffer.from(JSON.stringify({
  pattern: 'queue name',
  data: 'your actual payload',
}));

...
channel.sendToQueue('my_queue_name', payload);
Related