Not clear how to use MQTT v5?
TotallyInformation opened this issue · 2 comments
TotallyInformation commented
I'm wanting to connect to Mosquitto from the browser using MQTT v5. But the documentation is not clear on how to do this, there don't appear to be any examples?
Here is what I have but it isn't clear if I'm using v5, certainly I am not seeing user properties that are being set from a publish sent in a v5 client application (I am using a Node-RED server and can see the v5 data there).
import mqttClient from 'https://cdn.jsdelivr.net/npm/u8-mqtt/esm/web/index.js'
let mqtt = mqttClient()
.with_websock('wss:my.server:8883')
.with_autoreconnect()
mqtt.connect()
.then(() => {
console.log('connected')
mqtt.subscribe_topic('test/*', (pkt, params, ctx) => {
console.log('topic packet', params, pkt, pkt.json())
})
mqtt.subscribe_topic('test', (pkt, params, ctx) => {
console.log('topic packet', params, pkt, pkt.json())
})
})
shanewholloway commented
There is a web-targeted demo in demo/web_v5.js
, although it would be clearer if it written as a standalone demo instead of sharing code between the NodeJS and Deno backends.
// Using the `web/v5.js` module directly excludes code for MQTT 3.1.1 (v4) support -- great for the web!
// Also, importing the `mqtt_v5` export ensures you get the protocol version you want.
import { mqtt_v5 } from 'https://cdn.jsdelivr.net/npm/u8-mqtt/esm/web/v5.js'
let mqtt = mqtt_v5()
.with_websock('wss://my.server:8883') // use the secure WebSocket url protocol
.with_autoreconnect()
mqtt.connect()
.then(() => {
console.log('connected')
mqtt.subscribe_topic('test/*', (pkt, params, ctx) => {
console.log('topic packet', params, pkt, pkt.json())
})
mqtt.subscribe_topic('test', (pkt, params, ctx) => {
console.log('topic packet', params, pkt, pkt.json())
})
})
TotallyInformation commented
Thanks.