Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LWT sent on normal disconnection #1597

Open
nrbrook opened this issue Apr 17, 2023 · 2 comments
Open

LWT sent on normal disconnection #1597

nrbrook opened this issue Apr 17, 2023 · 2 comments

Comments

@nrbrook
Copy link

nrbrook commented Apr 17, 2023

I have the following typescript code, using MQTT.js 4.3.7:

private init():void {
        this.client = mqtt.connect(`mqtt://${process.env.BROKER_API}`, {
            will: {
                topic: "kfobdmsg/server/status",
                payload: "offline-unexpected",
                qos: 1,
                retain: true
            },
            clean: false,
            clientId: "kfobdmsg-server",
            username: process.env.BROKER_USERNAME,
            password: process.env.BROKER_PASSWORD
        });
        this.client.on("connect",()=>{
            console.log("Connected to broker!")
            this.client.publish("kfobdmsg/server/status", "online", {qos:1, retain:true});
        });
        this.client.on("error", (error:any) => {
            console.error("MQTT connection error:", error);
        });
    }
    
    public shutdown(complete: (result: boolean) => void): void {
        let success = true;
        console.log("Shutting down MQTT connection...");
        this.client.publish("kfobdmsg/server/status", "offline", {qos:1, retain:true}, (error:any)=>{
            if(error) {
                console.error("Error publishing offline message:", error);
                success = false;
            }
            console.log("Published offline message!");
            this.client.end(false, {
                reasonCode: 0,
                properties: {
                    reasonString: "Server shutting down"
                }
            }, (error:any)=>{
                if(error) {
                    console.error("Error disconnecting from broker:", error);
                    success = false;
                }
                console.log("Disconnected from broker!");
                complete(success);
            });
        });
        
    }

When shutdown is called, I get the following console output:

Shutting down MQTT connection...
Published offline message!
Disconnected from broker!

However, observing the published messages shows "offline" is published (correct) followed by "offline-unexpected"? Why is the LWT sent on normal connection close? I've attached a Wireshark trace.

mqtt capture.pcapng.zip

@nrbrook
Copy link
Author

nrbrook commented Apr 17, 2023

For clarity, attached another capture with no active subscribers. I can't see a disconnect message?

mqtt capture no observer.pcapng.zip

@robertsLando
Copy link
Member

robertsLando commented Jun 26, 2023

Try reproduce this with also broker in the same script. You can use Aedes broker for example. Once you reproduce it in a simple script you can also try to do a debug on mqttjs side. The LWT is sent by the broker when he detects an unexpected close of connection by the client (disconnect packet isn't received and ack)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants