MQTT.js icon indicating copy to clipboard operation
MQTT.js copied to clipboard

disconnect event did not being triggered while network being closed

Open laxian opened this issue 2 years ago • 3 comments

As the title says

this is my test code:

const mqtt = require('mqtt')

const host = 'broker.emqx.io'
const port = '1883'
const clientId = `S1RRR2232D0003_1`

const connectUrl = `mqtt://${host}:${port}`
const topic = "test-js"

const client = mqtt.connect(connectUrl, {
    clientId,
    clean: true,
    connectTimeout: 5 * 1000,
    reconnectPeriod: 5 * 1000,
    keepalive: 10,
})

client.on('connect', function () {
    client.publish(topic, 'Heartbeat Messages', { qos: 0, retain: false }, function (error) {
        if (error) {
            console.error(error)
        }
        else {
            console.log('Heartbeat Messages Sent.')
        }
    })
})

client.on('disconnect', function (error) {
    if (error) {
        console.error(error)
    }
})

client.on('offline', function (error) {
        console.error(error)
})

image

Why disconnect and offline events not being triggered? Thank you!

laxian avatar Mar 13 '23 06:03 laxian

on Ubuntu or Mac In case I start the test process on WIFI named ap1, I won't receive offline event if I switch WIFI to ap2 or turn off WIFI, until I reconnect to ap1

For comparison, if you use Python paho-mqtt library to switch or close WIFI, you can receive on_ disconnect, In addition, switching WIFI can correctly reconnect successfully

laxian avatar Mar 15 '23 07:03 laxian

This is an automated message to let you know that this issue has gone 365 days without any activity. In order to ensure that we work on issues that still matter, this issue will be closed in 14 days.

If this issue is still important, you can simply comment with a "bump" to keep it open.

Thank you for your contribution.

github-actions[bot] avatar Mar 15 '24 01:03 github-actions[bot]

@laxian are you able to reproduce the issue with latest version?

robertsLando avatar Mar 15 '24 07:03 robertsLando

@laxian are you able to reproduce the issue with latest version?

It's been happening to me. I haven't been getting disconnect events when the socket goes offline in the browser.

Edit: Disregard! I wasn't using keepAlive properly.

jzombie avatar Jul 30 '24 20:07 jzombie

I'm quite sure this is fixed now, be sure to setup the options correctly

robertsLando avatar Jul 31 '24 06:07 robertsLando