shanewholloway/js-u8-mqtt

Are connection options supported?

scruffymongrel opened this issue · 2 comments

With a bit of luck I'm hoping to replace mqtt.js with js-u8-mqtt but will need to be able to provide credentials, etc. when connecting to a third-party broker. Does the library support connection options like MQTT.js does? Something like:

const client = mqtt.connect('mqtts://broker.host.tld', {
    clientId: 'xxxxxxxxxx',
    password: 'xxxxxxxxxx',
    port: 8888,
    protocolVersion: 5,
    username: 'xxxxxxxxxx'
})

Apologies if this is a dumb question, but I haven't been able to find anything in the docs/code and just wanted to make sure I didn't miss anything.

This should be achievable this in combination with reading the specification and experimentation; the complexity will vary depending upon the server implementation specifics.

First, a bit of background. This project, u8-mqtt, started out as a little client wrapper around u8-mqtt-packet codec I wrote as an ES5 tree-shakable module implementation of the MQTT packet protocol versions. Thus, the .connect() method is a lite wrapper around creation of the underlying MQTT connect packet detailed in MQTT Connect packet codec of the js-u8-mqtt-packet project.

Hopefully it is as easy as providing username and password fields!

import { mqtt_v5 } from 'u8-mqtt/esm/node/v5.js'

const my_mqtt = mqtt_v5({on_live})
  .with_tcp('mqtts://broker.host.tld:8888')
  .with_autoreconnect()

async function on_live(my_mqtt) {
  await my_mqtt.connect({
    client_id: ['prefix', 'suffix'],
    username, 
    password,
  })
}

If your server proves more sophisticated, enhanced authentication options is one of the major extensions in MQTTv5. While v5 retains username and password fields, the props variable length field greatly extends the protocol. Section 3.1.2.11 CONNECT Properties of the MQTTv5 Specification details the usable properties for a connect packet, with addition details in section 4.12 Enhanced authentication. Note that there will be a required .auth() packet after the .connect() packet; this two-step authentication has not been used much by me, so I'm not sure how programmer friendly the library will be -- especially in combination with auto-reconnect features.

Thanks for the swift and thorough response -- very helpful! Unfortunately I forgot to mention I'm working in the browser.

Your explanation helped me get through some pretty cryptic third party docs, which means everything's working over websockets.

Thanks again for your help :)