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
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,
};
}
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):