`userProperties`在mqtt.js的订阅客户端上找不到

huangapple go评论54阅读模式
英文:

`userProperties` is not found on mqtt.js subscriber client

问题

我正在使用 `MQTT.js` 实现 MQTT 发布者和订阅者。
我使用 HiveMQ Cloud 作为代理。

当我通过发布者发送带有 `user property` 的数据包时,我期望我的订阅者能够接收到带有 `user property` 的数据包,但实际上没有。

以下是我的代码。第一个是发布者,第二个是订阅者。

```js
import { connect } from "mqtt";

const connectOptions = {
  host: 'hive cloud',
  port: 8883,
  protocol: 'mqtts',
  username: 'username',
  password: 'password'
}
let client = connect(connectOptions)

client.on("connect", (connack) => {
  console.log("client connected", connack);
})

setInterval(() => {
  const payload = {1: "Hello world", 3: "Welcome to the test connection"}
  const publishOption = {
    qos: 1,
    retain: false,
    properties: {
      userProperties: {
        user: 'property'
      }
    }
  }
  client.publish('test/connection708', JSON.stringify(payload), publishOption)
}, 5000)

.

import { connect } from "mqtt";

const connectOptions = {
  host: 'hive cloud',
  port: 8883,
  protocol: 'mqtts',
  username: 'username',
  password: 'password'
}

const topicName = 'test/connection708'

export let client = connect(connectOptions)

client.on('connect', (connack) => {
    console.log(connack)
    client.subscribe(topicName, (err, granted) => {
        if (err) {
            console.log(err, 'err')
        }
        console.log(granted, 'granted')
    })
})

client.on('message', (topic, message, packet) => {
    console.log(packet, message.toString())
})

我通过订阅者收到的数据包如下:
> Packet {
cmd: 'publish',
retain: false,
qos: 0,
dup: false,
length: 76,
topic: 'test/connection708',
payload: <Buffer 7b 22 31 22 3a 22 48 65 6c 6c 6f 20 77 6f 72 6c 64 22 2c 22 33 22 3a 22 57 65 6c 63 6f 6d 65 20 74 6f 20 74 68 65 20
74 65 73 74 20 63 6f 6e 6e 65 63 ... 6 more bytes>
}

数据包的载荷是 { "1": "Hello world", "3": "Welcome to the test connection" }

订阅者成功接收到了数据包及其载荷,但我找不到我提供的 user property,即对象 {user: 'property'}


<details>
<summary>英文:</summary>

I&#39;m implementing MQTT publisher and subscriber using `MQTT.js`.
And I&#39;m using HiveMQ Cloud as a broker.

When I sent packet with `user property` by the publisher, I expected my subscriber to receive the packet with `user property`, but it didn&#39;t.

Here&#39;s my code. First one is the publisher, and the next one is the subscriber.

```js
import { connect } from &quot;mqtt&quot;;

const connectOptions = {
  host: &#39;hive cloud&#39;,
  port: 8883,
  protocol: &#39;mqtts&#39;,
  username: &#39;username&#39;,
  password: &#39;password&#39;
}
let client = connect(connectOptions)

client.on(&quot;connect&quot;, (connack) =&gt; {
  console.log(&quot;client connected&quot;, connack);
})

setInterval(() =&gt; {
  const payload = {1: &quot;Hello world&quot;, 3: &quot;Welcome to the test connection&quot;}
  const publishOption = {
    qos: 1,
    retain: false,
    properties: {
      userProperties: {
        user: &#39;property&#39;
      }
    }
  }
  client.publish(&#39;test/connection708&#39;, JSON.stringify(payload), publishOption)
}, 5000)

.

import { connect } from &quot;mqtt&quot;;

const connectOptions = {
  host: &#39;hive cloud&#39;,
  port: 8883,
  protocol: &#39;mqtts&#39;,
  username: &#39;username&#39;,
  password: &#39;password&#39;
}

const topicName = &#39;test/connection708&#39;

export let client = connect(connectOptions)

client.on(&#39;connect&#39;, (connack) =&gt; {
    console.log(connack)
    client.subscribe(topicName, (err, granted) =&gt; {
        if (err) {
            console.log(err, &#39;err&#39;)
        }
        console.log(granted, &#39;granted&#39;)
    })
})

client.on(&#39;message&#39;, (topic, message, packet) =&gt; {
    console.log(packet, message.toString())
})

The packet I got through subscriber was same as down here:
> Packet {
cmd: 'publish',
retain: false,
qos: 0,
dup: false,
length: 76,
topic: 'test/connection708',
payload: <Buffer 7b 22 31 22 3a 22 48 65 6c 6c 6f 20 77 6f 72 6c 64 22 2c 22 33 22 3a 22 57 65 6c 63 6f 6d 65 20 74 6f 20 74 68 65 20
74 65 73 74 20 63 6f 6e 6e 65 63 ... 6 more bytes>
}

And the packet payload was {&quot;1&quot;:&quot;Hello world&quot;,&quot;3&quot;:&quot;Welcome to the test connection&quot;}

The subscriber successfully received packet and its payload, but I couldn't find the user property I gave, which is the object {user: 'property'}.

HiveMQ supports MQTT v5. But Just in case, I tried with local HiveMQ broker, but it didn't work.

答案1

得分: 1

您需要明确告知客户端使用MQTT v5进行连接,并使用MQTT v5的功能,因为默认情况下它将使用`protocolVersion` 4(对应3.1版本)。

const connectOptions = {
host: 'hive cloud',
port: 8883,
protocol: 'mqtts',
username: 'username',
password: 'password',
protocolVersion: 5
}


<details>
<summary>英文:</summary>

You will need to explicitly tell the client to connect using MQTT v5, to use  MQTT v5 features, since by default it will use `protocolVersion` 4 (which it 3.1).

const connectOptions = {
host: 'hive cloud',
port: 8883,
protocol: 'mqtts',
username: 'username',
password: 'password',
protocolVersion: 5
}


</details>



huangapple
  • 本文由 发表于 2023年3月8日 14:49:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定