@MessagePattern-Decorator with MQTT-Transport in nestjs

123 views Asked by At

I've been struggling with the following problem for days now and have already searched the nestjs documentation. I would like to equip several functions with the @MessagePattern decorator, but they all listen to the same topic. The functions should be called specifically when the payload has a specific property with a specific value. For this I wrote an interceptor and put it in the pipe. That would have worked so far. The problem is that unfortunately the @MessagePattern decorator only fires on one function. In the following example, without interceptor as filter, unfortunately only the last function fires. So I thought that you have to somehow get it to tell the @MessagePattern decorator to pre-filter on certain properties. The example from the documentation @MessagePattern({cmd: 'awesomeCommand'}) didn't work so far. However, I also ask myself the question of how the MQTT microservice knows which topic it should listen to? I hope you can help me.

I try:

some controller:

@MessagePattern('Test') // I tried @MessagePattern({cmd: 'testCommand1'}) but does'nt work
  async test1(payload: any) {
    throw new NotImplementedException('Test1');
  }
  @MessagePattern('Test')// I tried @MessagePattern({cmd: 'testCommand2'}) but does'nt work
  async test2(payload: any) {
    throw new NotImplementedException('Test2');
  }
  @MessagePattern('Test')// I tried @MessagePattern({cmd: 'testCommand3'}) but does'nt work
  async test3(payload: any) {
    throw new NotImplementedException('Test3'); // only this function fires, but why?
  }

my preferences in main.ts:

app.connectMicroservice<MicroserviceOptions>({
      transport: Transport.MQTT,
      options: {
        deserializer: new InboundRequestDeserializer(),
        serializer: new OutboundResponseSerializer(),
        path: 'Test'
      },
    });

and I expected all functions “test1()”, “test2” and “test3” to be called..

0

There are 0 answers