Skip to content

[Bug]: WebSocket not completely disconnected #1817

@ogis-yamazaki

Description

@ogis-yamazaki

MQTTjs Version

5.4.0

Broker

mosquitto v2.0.18

Environment

Browser

Description

I executed the mqtt.Client#end() function with the force option on the Websocket connection.
But it doesn't seem to be actually disconnected.

It happens in the browser, not in the Node program

The following sample code did not send a Will message.
Not when the client disconnects, Will message was sent when the broker disconnects after keep_alive seconds.

Minimal Reproduction

u1 user is monitoring Will message arrival while u2 is forcibly disconnecting

u1

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">
    <title>user 1</title>
</head>
<body>
    <script src="https://unpkg.com/[email protected]/dist/mqtt.js"></script>
    <script>
        const connectOptions = {
            keepalive: 300,
            clean: true,
            clientId: 'u1',
            reconnectPeriod: 0
        }

        const uri = 'ws://127.0.0.1:8080'
        const client = mqtt.connect(uri, connectOptions);

        client.on('message', (topic, message, packet) => {
            console.warn(`topic=${topic}, qos=${packet.qos}, pid=${packet.messageId}, message=${message}`)
        })

        client.once('connect', (packet) => {
            console.info(`connect event. packet = ${JSON.stringify(packet)}`)
            client.subscribe('t1', { qos: 1 }, (err, granted) => {
                if (!err) {
                    console.info(`subscribed. granted:${JSON.stringify(granted)}`)
                }
            })
        });
    </script>
</body>
</html>

u2

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">
    <title>user 2</title>
</head>
<body>
    <script src="https://unpkg.com/[email protected]/dist/mqtt.js"></script>
    <script>
        const num = Date.now()
        const connectOptions = {
            keepalive: 60, // 60 sec
            clean: true,
            clientId: 'u2',
            reconnectPeriod: 0
        }
        connectOptions.will =  {topic: 't1', qos: 1, payload: `u2 is dead. ${num}`}

        const uri = 'ws://127.0.0.1:8080'
        const client = mqtt.connect(uri, connectOptions);

        client.on('close', () => {
            console.info(`close event`)
        });
        client.on('error', (err) => {
            console.info(`error event. err = ${err}`)
        });
        client.on('end', () => {
            console.info(`end event`)
        });

        client.once('connect', (packet) => {
            console.info(`connect event. packet = ${JSON.stringify(packet)}`)
            setTimeout(() => {
                console.warn(`force disconnect`)
                client.end(true)
            }, 5000) // after 5sec
        });
    </script>
</body>
</html>

environment

  • mosquitto v 2.0.18
  • mqtt.js 5.4.0

Debug logs

--

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions