MQTT.js
MQTT.js copied to clipboard
disconnect event did not being triggered while network being closed
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)
})
Why disconnect and offline events not being triggered? Thank you!
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
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.
@laxian are you able to reproduce the issue with latest version?
@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.
I'm quite sure this is fixed now, be sure to setup the options correctly