Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write to stream fails when "userProperties" is an empy object #149

Open
br0wn opened this issue Aug 26, 2024 · 1 comment
Open

Write to stream fails when "userProperties" is an empy object #149

br0wn opened this issue Aug 26, 2024 · 1 comment

Comments

@br0wn
Copy link

br0wn commented Aug 26, 2024

While using protocol version 5, calling writeToStream with userProperties being empty object {} method fails with false as return value.
This is the packet being written:

{
  cmd: "publish",
  topic: "protocol-version-debug",
  payload: "{\"pattern\":\"protocol-version-debug\",\"data\":\"{\\\"ok\\\":1724704578895}\"}",
  qos: 0,
  retain: true,
  messageId: 0,
  dup: false,
  properties: {
    userProperties: {
    },
  },
}

I traced the problem to getProperties method which returns false when userProperties is empty object {} because getLengthProperty for empty object returns 0 causing getProperties to return false:

if (!propValueLength) { return false }

Setting userProperties to something like {'f': 'u'} removes the issue.

We are using ClientMqtt from @nestjs/microservices to emit data to a topic.

const {
	MqttRecordBuilder,
	ClientProxyFactory,
	Transport
} = require('@nestjs/microservices');

const client = ClientProxyFactory.create({
	transport: Transport.MQTT,
	options: {
		url: 'mqtt://0.0.0.0:1883',	
		protocolVersion: 5,	
	}
})

const record = new MqttRecordBuilder()
	.setData(JSON.stringify({ ok: new Date().getTime() }))
	.setRetain(true)
	.build();

client.emit('protocol-version-debug', record);

It seems the ClientMqtt sets userProperties to empty object when no userProperties is provided:

https://github.com/nestjs/nest/blob/e4b07eb06b406183e708269e0439f442a1dadb1c/packages/microservices/client/client-mqtt.ts#L217

@mcollina
Copy link
Member

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@mcollina @br0wn and others