Search code examples
nestjsmicroservicesnestjs-microservice

NestJS microservice not sending message via .send


I'm new to @nestjs/microservices, and running into a weird issue.

I have a setup of two apps written in Nest and checking the micro-service connection between them. For some strange reason, clientProxy.emit / @EventPattern works - but clientProxy.send / @MessagePattern is not. I've double checked the connection, it works fine. But the request/response approach does not work for me.

Any help would be greatly appreciated.. Sending the request is as follows:

this.shortenerService
      .send('create', {
        url: 'test',
      });

client code

Receiving:

  @UsePipes(new ValidationPipe())
  @MessagePattern('create')
  async create(@Payload() data: CreateDto): Promise<string> {
    return Promise.resolve(`${Math.random()}`);
  }

server code

Tried to change to .emit in client and @EventPattern in server - and it works. But not for request/response.

Tried to organize into a minimal repository


Solution

  • Nest's microservice ClientProxy uses observables, and send are cold observables, so you need to either return the response of .send for Nest to later subscribe, use .subscribe() yourself to kick off the observable, or convert the observable to a promise using lastValueFrom so you can await the response