pji114
pji114

Reputation: 31

How to mqtt publish on NestJS?

I am making mqtt client using NestJS

I used @MessagePattern to confirm the subscription. (@Ctx, @Payload information is also checked normally.)

By the way, mqtt client information is created in app.module I tried to use MqttClient using @Inject in Controller The following error occurs in client.push.

Help me (The controller's client.publish does not work.)

[main.ts]

async function bootstrap() {
  /*const app = await NestFactory.create(AppModule);
  await app.listen(3000);*/

  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
      AppModule,{
        transport:Transport.MQTT,
        options:{
          url : 'serverIp',
          username : 'name',
          password : 'pwd',
          port : 1883,
          protocol : 'mqtt'
        }
      },
  );

  app.listen(() =>{
    console.log('[Agent Server is Listening...]');
  })

}
bootstrap();

[app.module.ts]

@Module({
  imports: [      
    ClientsModule.register([
      {
        name: 'MQ_CLIENT',
        transport: Transport.MQTT,
        options: {
          host:'serverIp',
          port:1883,
          //protocol:'mqtt',
          username:'name',
          password:'pwd'
        }
      },
    ]),
    ConfigModule
  ],
  controllers: [AppController, VcController],
  providers: [AppService, VcService],
})
export class AppModule {}

[Controller]

@Controller('vc')
export class VcController {

    constructor(
        @Inject(WINSTON_MODULE_NEST_PROVIDER) private readonly logger : LoggerService,
        @Inject('MQ_CLIENT') private client : MqttClient
    ) {}

    @MessagePattern('SomeTopic)
    private tempFunction(@Ctx() context: MqttContext, @Payload() data){
      
        this.client.publish('Publish TopicId', 'tttaaaaaa'); //The problem arises here
    }
}

[Error Messgae]

TypeError: callback is not a function
    at ClientMqtt.publish (C:\Wrok_Git\Demo\20201013_nestjsMqtt\mqtt-agent-02\node_modules\@nestjs\microservices\client\client-mqtt.js:104:13)
    at VcController.vcTest (C:\Wrok_Git\Demo\20201013_nestjsMqtt\mqtt-agent-02\dist\controller\vc\vc.controller.js:40:21)
    at C:\Wrok_Git\Demo\20201013_nestjsMqtt\mqtt-agent-02\node_modules\@nestjs\microservices\context\rpc-context-creator.js:44:33
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async C:\Wrok_Git\Demo\20201013_nestjsMqtt\mqtt-agent-02\node_modules\@nestjs\microservices\context\rpc-proxy.js:11:32
    at async ServerMqtt.handleEvent (C:\Wrok_Git\Demo\20201013_nestjsMqtt\mqtt-agent-02\node_modules\@nestjs\microservices\server\server.js:61:32)

Upvotes: 3

Views: 6071

Answers (3)

Ashutosh Mishra
Ashutosh Mishra

Reputation: 21

This works for me -

constructor(@Inject('MQ_CLIENT') private client: ClientProxy) {
    client.connect();
}

@MessagePattern('SomeTopic')
private tempFunction(@Ctx() context: MqttContext, @Payload() data) {
    this.client.emit('help', 'data');
}

Upvotes: 2

Kleber Mar&#231;al
Kleber Mar&#231;al

Reputation: 1

The code of question is all OK. Only replace the line this.client.publish('Publish TopicId', 'tttaaaaaa'); with this.client.emit('Publish TopicId', 'tttaaaaaa')

Upvotes: -2

xtrinch
xtrinch

Reputation: 2291

I did a little digging and came up with the following:

https://github.com/nestjs/nest/issues/6403

The gist of it, you need to call mqttClient.connect() beforehand, but you will not find that function in the interface for it.

Upvotes: 0

Related Questions