mqttnestjs

Nestjs - There is no matching event handler defined in the remote service


I'm trying to handle the message published on topic test_ack from online MQTT broker using microservices. But I'm getting the error.

There is no matching event handler defined in the remote service.

My Code:

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Transport } from '@nestjs/common/enums/transport.enum';
var url = 'mqtt://test.mosquitto.org';

async function bootstrap() {
    const app = await NestFactory.createMicroservice(AppModule, {
        transport: Transport.MQTT,
        options: {
            url: url
        }
    });
    await app.listenAsync();
}
bootstrap();

app.controller.ts

import { Controller } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';

@Controller()
export class AppController {
    constructor() {}

    @MessagePattern('test') 
    ackMessageTestData(data:unknown) {
        console.log(data.toString());
        return 'Message Received';
    }
}

Solution

  • As I don't have edit permission, I will post it as a new answer. As mentioned in the above answer. We have to use @EventPattern('test_ack').

    The published message should be in format {data: 'Your message'} and should be serialized before publishing as mentioned here.

    client.publish('test_ack', JSON.stringify({data: 'test data'})).