eclipse / paho.mqtt.javascript

paho.mqtt.javascript
Other
1.14k stars 468 forks source link

`keepAliveInterval` does not seems to have any effect #132

Closed michaeljota closed 6 years ago

michaeljota commented 6 years ago

The property does not seems to have any effect and the client disconnect itself after one minute.

I set it to a value of 600, that according documentation, should be 10 minutes, as it should express the values in seconds, but after one minute approximately the client disconnect itself.

badcock4412 commented 6 years ago

Michael,

The keepAlive interval expressed by the client at connection time refers specifically to the keep alive functionality within MQTT. It's documented here: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc385349238. TL;DR The Keep Alive Interval is not a window for which the client must keep itself alive; it is a window that the server must kill the connection after if the server does not hear from the client.

There are many reasons why the client and/or server may kill a connection - the MQTT protocol allows wide discretion here, especially where conservation of resources are concerned. The keep alive feature specifically is where the server will kill the TCP connection to the client if it does not receive any data from the client within one-and-a-half times the keep alive interval. It is up to the client to ensure some data is sent every (keep alive interval) seconds, and in the absence of other traffic that is a PINGREQ. The Paho JS client does exactly this. Note that, per the documentation, the client does not need to wait (keep alive interval) seconds for a response from the server- it only needs to wait a "reasonable time". This implementation however will wait for (keep alive seconds).

With the MQTT Broker & environment I work in, the most common cases for the server killing connections to the client are failure to properly authenticate and use of the same client ID by two different. You may be able to get more information about why the client is closing through the .getTraceLog() method and by referring to these error codes: https://github.com/eclipse/paho.mqtt.javascript/blob/e3df35800acd425efd51c4f449481ccf9769e92e/src/paho-mqtt.js#L179

michaeljota commented 6 years ago

Thanks. I figure that out from try and error. I guest the problem is that if should reconnect itself and it currently does not.

corey-mitchell commented 4 years ago

I am actually having an issue where setting the keepAliveInterval option seems to do nothing. Despite being set to 5 seconds, if the client disconnects say from a faulty internet connection, then the broker does not disconnect for at least 60 seconds... I know this because I do not receive my LWT message for at least a minute. How can I make sure that the broker will send the LWT message immediately after internet connection has been severed?

My mqtt code:

           // Create MQTT Client
           const mqtt = new Paho.Client(config.REACT_APP_MQTT_DOMAIN, parseInt(config.REACT_APP_MQTT_PORT), "platform_" + parseInt(Math.random() * 100, 10));

            // Create Last will message
            var lastWill = new Paho.Message(JSON.stringify({
                action: 'Disconnect',
                data: robotUID
            }));
            lastWill.destinationName = 'lastWill/platform';
            lastWill.qos = 2;

            // Create Connection Options
            const options = {
                useSSL: true,
                userName: config.REACT_APP_MQTT_USERNAME,
                password: config.REACT_APP_MQTT_PASSWORD,
                onSuccess: onConnect,
                onFailure: doFail,
                willMessage: lastWill,
                keepAliveInterval: 5,
                reconnect: false
            };
            mqtt.onConnectionLost = onConnectionLost;

            // Connect to Broker
            mqtt.connect(options);

            // Handle Connect
            function onConnect() {
                console.log('MQTT Client Connected');
            };

            // Handle Fail
            function doFail(e){
                console.log('MQTT Connection Failed');
                console.log(e);
            };

            // Handle Connection Lost
            function onConnectionLost(responseObject) {
                console.log("onConnectionLost: " + responseObject.errorMessage);
                if(responseObject.errorCode !== 0) {
                    // Do something
                };
            };
badcock4412 commented 4 years ago

Corey,

Is this MQTT 5.0? You probably should look at the broker if so. MQTT-3.1.2-21 essentially states that the server holds the right to set the KeepAlive interval in the CONNACK message, and if it does, the client must accept it. So, the server can override you. I’d check that first. Either dig through your broker’s documentation or do a wire trace and see if byte 19 in the CONNACK message is set. The client library may also allow you to observe the negotiated keep alive? Worth a try too.