shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
552 stars 179 forks source link

[QUESTION] What is keepAlivePeriod? #554

Open rpungin opened 3 months ago

rpungin commented 3 months ago

Dear @shamblett, from your docs I see:

  /// Keep alive period, seconds.
  /// Keep alive is defaulted to off, this must be set to a valid value to
  /// enable keep alive.
  int keepAlivePeriod = MqttClientConstants.defaultKeepAlive;

But what exactly is "keep alive" in this context and what should the value of this parameter be for a Flutter app? I have seen examples where this value was set to 30 and some examples where it is set to 1200.

Is this parameter value also dependent on some other parameters, like autoReconnect? I think you mentioned that autoReconnect should be set to false for Flutter mobile apps and reconnection should be managed manually. Is that correct?

shamblett commented 3 months ago

These settings are nothing to with flutter or your application being a mobile application, they are part of the MQTT specification, see sections 3.12 and 3.13 or just type 'keepalive' in the search bar of the package API.

Autoreconnect can be set to anything you want for your use case, the platform makes no difference.

What I said was autoreconnect was not intended to recover from known platform events like foreground/background its more intended for sudden network/broker failure, some users use this, some users prefer to use keepalive to monitor broker/network availability, again your preference.

What issue are you reporting here? If your saying the package API documentation is unclear/wrong in these areas then please document this and I'll have a look at it.

rpungin commented 3 months ago

Thank you for the link to the specification, @shamblett !

Here is the extract from it in case someone else is looking for this information.

The Keep Alive is a time interval measured in seconds. Expressed as a 16-bit word, it is the maximum time interval that is permitted to elapse between the point at which the Client finishes transmitting one Control Packet and the point it starts sending the next. It is the responsibility of the Client to ensure that the interval between Control Packets being sent does not exceed the Keep Alive value. In the absence of sending any other Control Packets, the Client MUST send a PINGREQ Packet [MQTT-3.1.2-23].

The Client can send PINGREQ at any time, irrespective of the Keep Alive value, and use the PINGRESP to determine that the network and the Server are working.

If the Keep Alive value is non-zero and the Server does not receive a Control Packet from the Client within one and a half times the Keep Alive time period, it MUST disconnect the Network Connection to the Client as if the network had failed [MQTT-3.1.2-24].

If a Client does not receive a PINGRESP Packet within a reasonable amount of time after it has sent a PINGREQ, it SHOULD close the Network Connection to the Server.

A Keep Alive value of zero (0) has the effect of turning off the keep alive mechanism. This means that, in this case, the Server is not required to disconnect the Client on the grounds of inactivity. Note that a Server is permitted to disconnect a Client that it determines to be inactive or non-responsive at any time, regardless of the Keep Alive value provided by that Client.

So my next question is:

What is the recommended value for keepAlive parameter for a Flutter app working with AWS IoT MQTT Broker? Should I disable it by setting it to zero or should I set it to some specific number and if so what that number should be based off?