shanewholloway/js-u8-mqtt

Exceptions in browser console when connection to MQTT lost

Closed this issue · 3 comments

gwww commented

When the library loses connection with the MQTT server I start getting exceptions every 2 seconds when it tries to reconnect. I first noticed this when my laptop went to sleep and I came back to see the console log filled with exceptions. I understand why it disconnects, but when the network is back it does not seem to reconnect.

I am using MQTT auth, it seems that the auth credentials need to be supplied on reconnect. I'm not sure how to do that.

I a bit of a newbie to node, npm, etc. I could not figure out how this works: import mqtt_opts_v5 from 'u8-mqtt-packet/esm/codec_v5_client.js'. I was trying to find the codec code. I can see it in the browser but no idea where it's coming from. [Edit: I just found the other repo with codecs, that explains a bit more :)]

I'm connecting to a Mosquito server.

I can make the problem happen quickly by turning off my laptop WiFi. The exceptions continue when I turn WiFi back on.

Here is my init code:

    constructor(mqtt_server: string, auth: MQTTAuth | undefined = undefined) {
        this.server = mqtt_server;
        this.auth = auth;
        this.mqtt = mqtt_client()
            .with_websock(this.server)
            .with_autoreconnect(2500,
                () => { console.log('reconnect in auto reconnect') },
                () => { console.log('error in auto reconnect') }
            )
   }

Here is one of the exceptions:

[[u8-mqtt error: on_live]] Error: [0x5] Connection refused, not authorized
    MQTTError base.jsy:7
    connect base.jsy:45
    on_live core.jsy:34
    conn_emit base.jsy:21
    set _conn.jsy:65
    with_websock core.jsy:124
    with_websock core.jsy:110
    promise callback*on_reconnect core.jsy:45
    on_disconnect core.jsy:50
    conn_emit base.jsy:21
    reset _conn.jsy:26
    onclose core.jsy:137
    with_websock core.jsy:131
    with_websock core.jsy:110
    promise callback*on_reconnect core.jsy:45
    on_disconnect core.jsy:50
    conn_emit base.jsy:21
    reset _conn.jsy:26
    onclose core.jsy:137
    with_websock core.jsy:131
    with_websock core.jsy:110
    promise callback*on_reconnect core.jsy:45
    on_disconnect core.jsy:50
    conn_emit base.jsy:21
    reset _conn.jsy:26
    onclose core.jsy:137
    with_websock core.jsy:131
    with_websock core.jsy:110
    promise callback*on_reconnect core.jsy:45
    on_disconnect core.jsy:50
    conn_emit base.jsy:21
    reset _conn.jsy:26
    onclose core.jsy:137
    with_websock core.jsy:131
    with_websock core.jsy:110
    promise callback*on_reconnect core.jsy:45
    on_disconnect core.jsy:50
    conn_emit base.jsy:21
    reset _conn.jsy:26
    onclose core.jsy:137
    with_websock core.jsy:131
    with_websock core.jsy:110
    promise callback*on_reconnect core.jsy:45
    on_disconnect core.jsy:50
    conn_emit base.jsy:21
    reset _conn.jsy:26
    onclose core.jsy:137
    with_websock core.jsy:131
    with_websock core.jsy:110
    promise callback*on_reconnect core.jsy:45
    on_disconnect core.jsy:50
    conn_emit base.jsy:21
gwww commented

I have verified that at https://github.com/shanewholloway/js-u8-mqtt-packet/blob/master/code/encode/connect.js#L25 both username and password are undefined.

gwww commented

Well it has been a journey of discovery! I hope that I didn't bother you too much. I did get it working. Once I learned about on_live that was my first clue. The next was figuring out that JS and Python are different when it comes to this/self in callbacks. In Python self refers to the called method not the callee method. Working code is here: https://github.com/gwww/z2m-frontend/blob/main/src/lib/mqtt.ts and here https://github.com/gwww/z2m-frontend/blob/main/src/lib/hook.client.ts.

I figured your code was doing the right thing. Its so well crafted! I learn bits from it every time I read it. JSY is a bit of a learning curve but I can usually see through it to what its supposed to be doing.

Apologies for bothering you and thanks for the library!

I'm so glad to have you using the library and finding it useful! No bother at all -- I was simply at work. Even better, I'm very happy you were able to find the answer. Coming from Python for the JSY spelunking you've been doing is great -- that's the code culture I came from as well.

I've spent the most focus implementing the MQTT packet parsers for version 4 (3.11) and version 5. I'd like to have more validation of the client and libraries against varied MQTT servers and configurations. For instance, the reconnection logic is a recent addition (v0.3), and you may be the first to test it with authentication settings.

All that said, please keep the questions, observations, and comments coming! They are welcome. I'll find the notes very useful feedback, and I'll be able to troubleshoot and poke at it some evenings and weekends. I'm very open to pull requests to improve docs, readme, examples, and code/bugfixes etc. If an explanation would have helped you, I'd love to add it for the next person. Also, if you think of a feature that'd make the library better, I'd love to have it in the issue tracker.