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

[help wanted] nothing receive after publishing message with MQTT5 properties #1357

Closed
oceanlvr opened this issue Nov 21, 2021 · 5 comments
Closed
Assignees

Comments

@oceanlvr
Copy link
Contributor

oceanlvr commented Nov 21, 2021

Environment:

  • MQTT.js version: ^4.2.8
  • Node 14
  • Tested on EMQX and HiveMQ broker

Dear dev, I write some demo to test publish API (with MQTT5 properties), and nothing was received when I set the IClientPublishOptions['properties']. Here is the code

const mqtt = require('mqtt')

const host = 'broker.mqttdashboard.com' // or broker.mqttdashboard.com
const port = '8000' // 8000 for broker.mqttdashboard.com
const protocol = 'ws' // ws for broker.mqttdashboard.com
const connectUrl = `${protocol}://${host}:${port}`

const clientId = `mqtt_${Math.random().toString(16).slice(3)}`

const client = mqtt.connect(connectUrl, {
  clientId,
  clean: true,
  connectTimeout: 4000,
  reconnectPeriod: 1000,
  protocolVersion: 5,
  path: '/mqtt', // for broker.mqttdashboard.com
})

const topic = '/nodejs/mqtt'

client.on('connect', () => {
  console.log('Connected')
  client.subscribe([topic], () => {
    console.log(`Subscribe to topic '${topic}'`)
    client.publish(topic, 'nodejs mqtt test', {
      qos: 2,
     // ⚠⚠ help wanted, properties affect on message event and we can't send message with properties successfully
      properties: {
        topicAlias: 'test',
      userProperties: {
        myprop: "myprop value"
      }
      },
      retain: false
    }, (error) => {
      if (error) {
        console.error(error)
      }
    })
  })
})

client.on('message', (topic, payload, packet) => {
  console.log(`=====Received Message=====
from topic: ${topic}
payload:    ${payload.toString()}
packet:     ${packet.toString()}
properties: ${packet.properties}
`)
})

publish properties affect message events and we can't send messages with properties successfully.
Nothing receives, and we expect at least a message like nodejs mqtt test, then I pub some other message on this topic, nothing receive either.
image

Did I miss any details? Or is it just a bug? Waiting for your reply. THANKS!! :)

Here is my test repo, please try this to get more detail:

https://github.com/oceanlvr/mqtt5-publish-props

Maybe relative:

@oceanlvr
Copy link
Contributor Author

I'm sorry to bother you, Did I miss any details? Or is it just a bug? Waiting for your reply. THANKS!! :) @YoDaMa

@oceanlvr
Copy link
Contributor Author

oceanlvr commented Nov 21, 2021

I also test on MQTT.js version: 4.0.0

and Also can't send an message with properties:

const mqtt = require('mqtt')

const host = 'broker.mqttdashboard.com' // or broker.mqttdashboard.com
const port = '8000' // 8000 for broker.mqttdashboard.com
const protocol = 'ws' // ws for broker.mqttdashboard.com
const connectUrl = `${protocol}://${host}:${port}`

const clientId = `mqtt_${Math.random().toString(16).slice(3)}`

const client = mqtt.connect(connectUrl, {
  clientId,
  clean: true,
  connectTimeout: 4000,
  reconnectPeriod: 1000,
  protocolVersion: 5,
  path: '/mqtt', // for broker.mqttdashboard.com
})

const topic = '/nodejs/mqtt'

client.on('message', (topic, payload, packet) => {
  console.log(`=====Received Message=====
from topic: ${topic}
payload:    ${payload.toString()}
packet:     ${JSON.stringify(packet)}
properties: ${packet.properties}
`)
})

client.on('connect', () => {
  console.log('Connected')
  client.subscribe([topic], () => {
    console.log(`Subscribe to topic '${topic}'`)
    client.publish(topic, 'nodejs mqtt test', {
      qos: 2,
      properties: {
        topicAlias: 'test',
        userProperties: {
          myprop: "myprop value"
        }
      },
      retain: false
    }, (error) => {
      if (error) {
        console.error(error)
      }
    })
  })
})

result

image


after delete properties:

client.on('connect', () => {
  console.log('Connected')
  client.subscribe([topic], () => {
    console.log(`Subscribe to topic '${topic}'`)
    client.publish(topic, 'nodejs mqtt test', {
      qos: 2,
-      properties: {
-      topicAlias: 'test',
-      userProperties: {
-      myprop: "myprop value"
-      }
-     },
      retain: false
    }, (error) => {
      if (error) {
        console.error(error)
      }
    })
  })
})

Message send normally.

image

@YoDaMa
Copy link
Contributor

YoDaMa commented Dec 13, 2021

@oceanlvr thanks for your patience. looking at this issue now.

There is a bug in the typings of topicAlias, it should be a number. This is fixed in this PR: https://github.com/mqttjs/MQTT.js/pull/1359/files

So make sure to use a number for your topicAlias.

@YoDaMa
Copy link
Contributor

YoDaMa commented Dec 27, 2021

@oceanlvr did you find this solved?

@oceanlvr
Copy link
Contributor Author

oceanlvr commented Jan 7, 2022

Yes, problem solved! :)

@oceanlvr oceanlvr closed this as completed Jan 7, 2022
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

2 participants