nest icon indicating copy to clipboard operation
nest copied to clipboard

When using the MqttClient Proxy in MQTT 5.0, we must call the setProperties() to emit message

Open erich2s opened this issue 1 year ago • 0 comments
trafficstars

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current behavior

// app.module.ts
import { Module } from '@nestjs/common';
import { AppService } from './app.service';
import { ClientsModule, Transport } from '@nestjs/microservices';
import { AppController } from './app.controller';

@Module({
  imports: [
    ClientsModule.register([
      {
        name: 'MQTT_SERVICE_V5',
        transport: Transport.MQTT,
        options: {
          url: 'mqtt://localhost:1883',
          protocolVersion: 5, // specify MQTT 5.0 protocol version
        },
      },
    ]),
  ],
  providers: [AppService],
  controllers: [AppController],
})
export class AppModule {}

If we comment out the .setProperties() method, the message won't be emitted

import { Inject, Injectable } from '@nestjs/common';
import { ClientProxy, MqttRecordBuilder } from '@nestjs/microservices';

@Injectable()
export class AppService {
  constructor(
    @Inject('MQTT_SERVICE_V5')
    private readonly mqttServiceV5: ClientProxy,
  ) {}

  sendMessageV5WithMqttRecordBuilder() {
    const data = new MqttRecordBuilder()
      .setData('Hello from MQTT 5.0 Client Proxy')
      // If we comment out this .setProperties(), the message won't be emitted
      .setProperties({ userProperties: { anyKey: 'anyValue' } })
      .setQoS(2)
      .build();
    this.mqttServiceV5.emit('chat', data);
  }
}

Minimum reproduction code

https://github.com/erich2s/mqttclient-emit-fail

Steps to reproduce

pnpm i
pnpm dev

Expected behavior

Not setting any properties when using MqttrecordBuilder to send the message in MQTT 5.0

Package

  • [ ] I don't know. Or some 3rd-party package
  • [ ] @nestjs/common
  • [ ] @nestjs/core
  • [X] @nestjs/microservices
  • [ ] @nestjs/platform-express
  • [ ] @nestjs/platform-fastify
  • [ ] @nestjs/platform-socket.io
  • [ ] @nestjs/platform-ws
  • [ ] @nestjs/testing
  • [ ] @nestjs/websockets
  • [ ] Other (see below)

Other package

No response

NestJS version

latest

Packages versions

"@nestjs/microservices": "^10.4.5"

Node.js version

20

In which operating systems have you tested?

  • [X] macOS
  • [ ] Windows
  • [ ] Linux

Other

MQTT Broker: EMQX 5.8.0

erich2s avatar Oct 19 '24 16:10 erich2s