1

I thought I would add keepalive: 120 to my Mqtt Broker connection code, but I am still getting disconnected by the Broker with the following message (as per my Chrome Network tab):

.M.K..HThe client was idle for too long without sending an MQTT control packet.

For example

enter image description here

Here is the connection string in my Angular 13 code (with keepalive: 120)

    getBrokerConnection(): IMqttServiceOptions {
        const d = new Date();
        const tempId =
            'HarBrowser' + (d.getUTCMinutes() + d.getUTCMilliseconds() + Math.random()).toFixed(8).toString();
        const username = atob(this.config.getConfig('mqttUser'));
        const password = atob(this.config.getConfig('mqttPass'));
        const brokerAddr = this.config.getConfig('mqttBrokerAddress');
        const port = this.config.getConfig('mqttPort');
        const protocol = this.config.getConfig('mqttProtocol');
        return {
            hostname: brokerAddr,
            port: 8884,
            path: '/mqtt',
            clean: true, // CleanSession=true does not persist the prev client session
            connectTimeout: 30000,
            keepalive: 120,
            reconnectPeriod: 0, // 0 to disable auto-reconn (to prevent many front-end errors)
            clientId: tempId,
            username: username,
            password: password,
            protocol: protocol,
            connectOnCreate: false,
            protocolVersion: 5,
        };
    }

enter image description here As I understood it, the Mqtt client is responsible for sending a pingreq to the broker in order to keep the connection alive (as per https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901045)

I do see this event logged to Chrome console many times, so I assumed it was my Mqtt client sending that pingreq as per the keepalive param:

 this.mqttService.onPacketsend.subscribe((res) => console.log(`onPacketsend event ${res}`)); 

Now am I supposed to programmatically send the pingreq, or is that supposed to be built into this Angular wrapper to mqtt.js ? I am a bit unclear as to what I am doing wrong, as I am getting disconnected by the broker.

** UPDATE ** In my backend Windows Service, it seems I can simulate the keepalive timeout issue if I set a break point and just let it sit there for a while. Otherwise it's not timing out on its own. For example,

KeepAliveTimeout The client was idle for too long without sending an MQTT control packet.

I set a breakpoint here in my local deployment of the service (an Mqtt client):

enter image description here

bob.mazzo
  • 5,183
  • 23
  • 80
  • 149
  • That WebSocket message is not a MQTT message (there is no way to include a textual error message in the header like that), it must be coming from something in your backend and this is not a MQTT over WebSocket conenction (as that would be all that is possible from a web browser) – hardillb May 17 '23 at 06:55
  • @hardillb I do have several mqtt clients running on multiple machines as a Windows Service. Perhaps as you said, one of them is "timing out" and then publishing that `idle for too long` message to one of my topics (which my browser app is subscribing to). I'll debug those services a bit further, thanks. – bob.mazzo May 17 '23 at 13:47
  • @hardillb if you see my screen shot (bottom of post), my browser does conn to `mqtt` via `wss`. Maybe there's something in my connection to the broker which allows that plain text situation. – bob.mazzo May 17 '23 at 14:10

0 Answers0