I am currently utilizing Nestjs for sending data to a Mqtt Broker. However, I am facing an issue where it sends both the pattern and data instead of just the data in this format:
{
"pattern": "test/test",
"data": "Data"
}
Within the app.module.ts
, I have imported the MQTT Client as follows:
@Module({
imports: [
ClientsModule.register([
{
name: 'MQTT_CLIENT',
transport: Transport.MQTT,
options: {
url: 'tcp://abc.abc.com',
username: 'name',
password: 'psswrd',
port: 1883,
},
},
]),
],
controllers: [AppController],
providers: [AppService],
})
and in the app.controller.ts
:
@Controller()
export class AppController {
constructor(@Inject('MQTT_CLIENT') private client: ClientMqtt) {}
async onApplicationBootstrap() {
await this.client.connect();
}
@Get()
getHello(): string {
this.client.emit('test/test', 'Data')
return 'Done';
}
}
Upon further investigation into the ClientMqtt
proxy source code, I discovered that the method publish
is designed to publish the entire partialPacket
which includes the JSON {pattern, data} rather than solely packet.data
protected publish(
partialPacket: ReadPacket,
callback: (packet: WritePacket) => any,
): Function {
try {
const packet = this.assignPacketId(partialPacket);
const pattern = this.normalizePattern(partialPacket.pattern);
...
this.mqttClient.publish(
this.getAckPatternName(pattern),
JSON.stringify(packet),
);
});
...
}
If my intention is to utilize the ClientMqtt
proxy but only publish the data field of the packet, what approach should I take?