shamblett / mqtt5_client

A server and browser based MQTT 5 client for dart
Other
49 stars 25 forks source link

Using the default keepalive interval should not result in keepalive being disabled #100

Open GeoffArmstrong opened 1 month ago

GeoffArmstrong commented 1 month ago

What I observed:

Every 90 seconds, my MQTT client disconnected and then reconnected to the broker.

Investigation:

I turned on logging. The reason given that MQTT was disconnecting was given as a keepalive timeout.

Looking at the connection logging indicated that keepalive was disabled for the connection.

I looked in the code to find the offending logging statement. I found this in lib/src/mqtt_client.dart:262-272:

    if (keepAlivePeriod != MqttConstants.defaultKeepAlive) {
      MqttLogger.log(
          'Mqtt5Client::connect - keep alive is enabled with a value of $keepAlivePeriod seconds');
      keepAlive = MqttConnectionKeepAlive(connectionHandler, clientEventBus,
          keepAlivePeriod, disconnectOnNoResponsePeriod);
      if (pongCallback != null) {
        keepAlive!.pongCallback = pongCallback;
      }
    } else {
      MqttLogger.log('Mqtt5Client::connect - keep alive is disabled');
    }

I believe the proper fix is that the first line should be if (keepAlivePeriod != 0) {.

Temporary fix:

I set the keepalive period to 59 seconds, and everything worked.

shamblett commented 1 month ago

I see what you are saying but I'm not seeing this behaviour, here's the log from the mqtt_server_client example with the async sleep time raised to 240 seconds.

/home/steve/Development/google/dart/dart-sdk/bin/dart --enable-asserts --no-serve-devtools /home/steve/Development/google/dart/projects/mqtt5_client/example/mqtt5_server_client.dart
EXAMPLE::Mqtt5 client connecting....
2024-07-09 09:36:18.176237 -- Mqtt5Client::connect - keep alive is disabled
2024-07-09 09:36:18.178460 -- MqttConnectionHandlerBase::connect - server test.mosquitto.org, port 1883
2024-07-09 09:36:18.179206 -- MqttSynchronousServerConnectionHandler::internalConnect entered
2024-07-09 09:36:18.179375 -- MqttSynchronousServerConnectionHandler::internalConnect - initiating connection try 0, auto reconnect in progress false
2024-07-09 09:36:18.179449 -- MqttSynchronousServerConnectionHandler::internalConnect - insecure TCP selected
2024-07-09 09:36:18.179812 -- MqttSynchronousServerConnectionHandler::internalConnect - calling connect
2024-07-09 09:36:18.180548 -- MqttNormalConnection::connect- entered
2024-07-09 09:36:18.481963 -- MqttServerConnection::_startListening
2024-07-09 09:36:18.483085 -- MqttSynchronousServerConnectionHandler::internalConnect - connection complete
2024-07-09 09:36:18.483106 -- MqttSynchronousServerConnectionHandler::internalConnect sending connect message
2024-07-09 09:36:18.483224 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.connect
MessageType = MqttMessageType.connect Duplicate = false Retain = false Qos = atMostOnce Size = 0
ProtocolName = MQTT
ProtocolVersion = 5
ConnectFlags = CleanStart=true, WillFlag=false, WillQos=atMostOnce, WillRetain=false, PasswordFlag=false, UserNameFlag=false
KeepAlive = 0
Properties = No properties setWill topic = null
User name = not set
Password = not set

2024-07-09 09:36:18.494836 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
2024-07-09 09:36:18.495190 -- MqttSynchronousServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of notSet and a disconnection origin of none
2024-07-09 09:36:18.526412 -- MqttServerConnection::_onData - Message Received Started <<< 
2024-07-09 09:36:18.526514 -- MqttServerConnection::_ondata - adding incoming data, data length is 11, message stream length is 0, message stream position is 0
2024-07-09 09:36:18.526573 -- MqttServerConnection::_ondata - added incoming data message stream length is 11, message stream position is 0
2024-07-09 09:36:18.526736 -- MqttByteBuffer:isMessageAvailable - assumed valid header, value is 32
2024-07-09 09:36:18.530954 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.connectAck
MessageType = MqttMessageType.connectAck Duplicate = false Retain = false Qos = atMostOnce Size = 9
Session Present = false
Connect Reason Code = success
Session Expiry Interval = 0
Receive Maximum = 10
Maximum QoS = 2
Retain Available = false
Maximum Packet Size = 0
Assigned client Identifier = null
Topic Alias Maximum = 10
Reason String = null
Wildcard Subscription Available = true
Subscription Identifiers Available = true
Shared Subscription Available = true
broker Keep Alive = 0
Response Information = null
broker Reference = null
Authentication Method = null
Properties = Identifier : topicAliasMaximum, value : 10
Identifier : receiveMaximum, value : 10

2024-07-09 09:36:18.532640 -- MqttServerConnection::_onData - message available event fired
2024-07-09 09:36:18.532658 -- MqttServerConnection::_onData - Message Received Ended <<< 
2024-07-09 09:36:18.533599 -- MqttConnectionHandlerBase::_connectAckProcessor
2024-07-09 09:36:18.533850 -- MqttConnectionHandlerBase::_reasonCodeOk - reason code is ok success
2024-07-09 09:36:18.533885 -- MqttConnectionHandlerBase::_connectAckProcessor - state = connected
EXAMPLE::OnConnected client callback - Client connection was successful
2024-07-09 09:36:18.534010 -- MqttConnectionHandlerBase:: cancelling connect timer
2024-07-09 09:36:18.534188 -- MqttSynchronousServerConnectionHandler::internalConnect - post sleep, state = Connection status is connected with return code of success and a disconnection origin of none
2024-07-09 09:36:18.534218 -- MqttSynchronousServerConnectionHandler::internalConnect exited with state Connection status is connected with return code of success and a disconnection origin of none
EXAMPLE::Mqtt5 server client connected, return code is success
EXAMPLE::Subscribing to the test/lol topic
2024-07-09 09:36:18.537119 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
Message Identifier = 1
Subscription identifier = 0
Properties = No properties set
Topic = test/lol, Option = Maximum Qos = atMostOnce
No Local = false
Retain As Published = true
Retain Handling = sendRetained

2024-07-09 09:36:18.538485 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
EXAMPLE::Subscribing to the Dart/Mqtt5_client/testtopic topic
2024-07-09 09:36:18.538712 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.subscribe
MessageType = MqttMessageType.subscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
Message Identifier = 2
Subscription identifier = 0
Properties = No properties set
Topic = Dart/Mqtt5_client/testtopic, Option = Maximum Qos = exactlyOnce
No Local = false
Retain As Published = true
Retain Handling = sendRetained

2024-07-09 09:36:18.538831 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
EXAMPLE::Sleeping....
2024-07-09 09:36:18.559864 -- MqttServerConnection::_onData - Message Received Started <<< 
2024-07-09 09:36:18.559899 -- MqttServerConnection::_ondata - adding incoming data, data length is 6, message stream length is 0, message stream position is 0
2024-07-09 09:36:18.559914 -- MqttServerConnection::_ondata - added incoming data message stream length is 6, message stream position is 0
2024-07-09 09:36:18.559923 -- MqttByteBuffer:isMessageAvailable - assumed valid header, value is 144
2024-07-09 09:36:18.561106 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.subscribeAck
MessageType = MqttMessageType.subscribeAck Duplicate = false Retain = false Qos = atMostOnce Size = 4
Message Identifier = 1
Reason String = null
Properties = No properties set
Reason Code = grantedQos0

2024-07-09 09:36:18.561578 -- MqttServerConnection::_onData - message available event fired
2024-07-09 09:36:18.561592 -- MqttServerConnection::_onData - Message Received Ended <<< 
2024-07-09 09:36:18.561756 -- MqttConnectionHandlerBase::messageAvailable - message type is MqttMessageType.subscribeAck
EXAMPLE::Subscription confirmed for topic test/lol
2024-07-09 09:36:18.581116 -- MqttServerConnection::_onData - Message Received Started <<< 
2024-07-09 09:36:18.581150 -- MqttServerConnection::_ondata - adding incoming data, data length is 6, message stream length is 0, message stream position is 0
2024-07-09 09:36:18.581165 -- MqttServerConnection::_ondata - added incoming data message stream length is 6, message stream position is 0
2024-07-09 09:36:18.581174 -- MqttByteBuffer:isMessageAvailable - assumed valid header, value is 144
2024-07-09 09:36:18.581270 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.subscribeAck
MessageType = MqttMessageType.subscribeAck Duplicate = false Retain = false Qos = atMostOnce Size = 4
Message Identifier = 2
Reason String = null
Properties = No properties set
Reason Code = grantedQos2

2024-07-09 09:36:18.581422 -- MqttServerConnection::_onData - message available event fired
2024-07-09 09:36:18.581431 -- MqttServerConnection::_onData - Message Received Ended <<< 
2024-07-09 09:36:18.581455 -- MqttConnectionHandlerBase::messageAvailable - message type is MqttMessageType.subscribeAck
EXAMPLE::Subscription confirmed for topic Dart/Mqtt5_client/testtopic
EXAMPLE::Publishing our topic now we are subscribed
2024-07-09 09:36:18.582612 -- MqttPublishingManager::publish - entered with topic Dart/Mqtt5_client/testtopic
2024-07-09 09:36:18.583529 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.publish
MessageType = MqttMessageType.publish Duplicate = false Retain = false Qos = exactlyOnce Size = 0
Topic Name = Dart/Mqtt5_client/testtopic
Message Identifier = 3
Payload Format Indicator = false
Message Expiry Interval = 65535
Topic Alias = 255
Response Topic = 
Subscription Identifier(s) = []
Properties = No properties set

Payload: {23 bytes={<72><101><108><108><111><32><102><114><111><109><32><109><113><116><116><53><95><99><108><105><101><110><116>

2024-07-09 09:36:18.584882 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
2024-07-09 09:36:18.604502 -- MqttServerConnection::_onData - Message Received Started <<< 
2024-07-09 09:36:18.604537 -- MqttServerConnection::_ondata - adding incoming data, data length is 4, message stream length is 0, message stream position is 0
2024-07-09 09:36:18.604551 -- MqttServerConnection::_ondata - added incoming data message stream length is 4, message stream position is 0
2024-07-09 09:36:18.604561 -- MqttByteBuffer:isMessageAvailable - assumed valid header, value is 80
2024-07-09 09:36:18.605216 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.publishReceived
MessageType = MqttMessageType.publishReceived Duplicate = false Retain = false Qos = atMostOnce Size = 2
Message Identifier = 3
Reason Code = success
Properties = No properties set

2024-07-09 09:36:18.605665 -- MqttServerConnection::_onData - message available event fired
2024-07-09 09:36:18.605679 -- MqttServerConnection::_onData - Message Received Ended <<< 
2024-07-09 09:36:18.605707 -- MqttConnectionHandlerBase::messageAvailable - message type is MqttMessageType.publishReceived
2024-07-09 09:36:18.605817 -- MqttPublishingManager::handlePublishReceived - entered
2024-07-09 09:36:18.606229 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.publishRelease
MessageType = MqttMessageType.publishRelease Duplicate = false Retain = false Qos = atLeastOnce Size = 0
Message Identifier = 3
Reason Code = success
Properties = No properties set

2024-07-09 09:36:18.606957 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
2024-07-09 09:36:18.627578 -- MqttServerConnection::_onData - Message Received Started <<< 
2024-07-09 09:36:18.627614 -- MqttServerConnection::_ondata - adding incoming data, data length is 57, message stream length is 0, message stream position is 0
2024-07-09 09:36:18.627630 -- MqttServerConnection::_ondata - added incoming data message stream length is 57, message stream position is 0
2024-07-09 09:36:18.627639 -- MqttByteBuffer:isMessageAvailable - assumed valid header, value is 52
2024-07-09 09:36:18.629948 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.publish
MessageType = MqttMessageType.publish Duplicate = false Retain = false Qos = exactlyOnce Size = 55
Topic Name = Dart/Mqtt5_client/testtopic
Message Identifier = 1
Payload Format Indicator = false
Message Expiry Interval = 65535
Topic Alias = 255
Response Topic = 
Subscription Identifier(s) = []
Properties = No properties set

Payload: {23 bytes={<72><101><108><108><111><32><102><114><111><109><32><109><113><116><116><53><95><99><108><105><101><110><116>

2024-07-09 09:36:18.630080 -- MqttServerConnection::_onData - message available event fired
2024-07-09 09:36:18.630091 -- MqttServerConnection::_onData - Message Received Ended <<< 
2024-07-09 09:36:18.630137 -- MqttConnectionHandlerBase::messageAvailable - message type is MqttMessageType.publish
2024-07-09 09:36:18.630406 -- MqttPublishingManager::handlePublish - entered
2024-07-09 09:36:18.630660 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.publishReceived
MessageType = MqttMessageType.publishReceived Duplicate = false Retain = false Qos = atMostOnce Size = 0
Message Identifier = 1
Reason Code = success
Properties = No properties set

2024-07-09 09:36:18.630971 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
2024-07-09 09:36:18.631024 -- MqttServerConnection::_onData - Message Received Started <<< 
2024-07-09 09:36:18.631035 -- MqttServerConnection::_ondata - adding incoming data, data length is 4, message stream length is 0, message stream position is 0
2024-07-09 09:36:18.631045 -- MqttServerConnection::_ondata - added incoming data message stream length is 4, message stream position is 0
2024-07-09 09:36:18.631053 -- MqttByteBuffer:isMessageAvailable - assumed valid header, value is 112
2024-07-09 09:36:18.631591 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.publishComplete
MessageType = MqttMessageType.publishComplete Duplicate = false Retain = false Qos = atMostOnce Size = 2
Message Identifier = 3
Reason Code = success
Properties = No properties set

2024-07-09 09:36:18.631812 -- MqttServerConnection::_onData - message available event fired
2024-07-09 09:36:18.631824 -- MqttServerConnection::_onData - Message Received Ended <<< 
2024-07-09 09:36:18.631843 -- MqttConnectionHandlerBase::messageAvailable - message type is MqttMessageType.publishComplete
2024-07-09 09:36:18.631916 -- MqttPublishingManager::handlePublishComplete - entered
2024-07-09 09:36:18.632011 -- MqttPublishingManager::_notifyPublish - entered message MqttQos.exactlyOnce
EXAMPLE::Published notification:: topic is Dart/Mqtt5_client/testtopic, with Qos MqttQos.exactlyOnce
2024-07-09 09:36:18.651914 -- MqttServerConnection::_onData - Message Received Started <<< 
2024-07-09 09:36:18.651948 -- MqttServerConnection::_ondata - adding incoming data, data length is 4, message stream length is 0, message stream position is 0
2024-07-09 09:36:18.651961 -- MqttServerConnection::_ondata - added incoming data message stream length is 4, message stream position is 0
2024-07-09 09:36:18.651971 -- MqttByteBuffer:isMessageAvailable - assumed valid header, value is 98
2024-07-09 09:36:18.652421 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.publishRelease
MessageType = MqttMessageType.publishRelease Duplicate = false Retain = false Qos = atLeastOnce Size = 2
Message Identifier = 1
Reason Code = success
Properties = No properties set

2024-07-09 09:36:18.652488 -- MqttServerConnection::_onData - message available event fired
2024-07-09 09:36:18.652498 -- MqttServerConnection::_onData - Message Received Ended <<< 
2024-07-09 09:36:18.652527 -- MqttConnectionHandlerBase::messageAvailable - message type is MqttMessageType.publishRelease
2024-07-09 09:36:18.652680 -- MqttPublishingManager::handlePublishRelease - entered
2024-07-09 09:36:18.652974 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.publishComplete
MessageType = MqttMessageType.publishComplete Duplicate = false Retain = false Qos = atMostOnce Size = 0
Message Identifier = 1
Reason Code = success
Properties = No properties set

2024-07-09 09:36:18.653290 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
2024-07-09 09:36:18.653501 -- MqttSubscriptionManager::publishMessageReceived topic is Dart/Mqtt5_client/testtopic
EXAMPLE::Change notification:: topic is <Dart/Mqtt5_client/testtopic>, payload is <-- Hello from mqtt5_client -->
EXAMPLE::Unsubscribing
2024-07-09 09:40:18.540815 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.unsubscribe
MessageType = MqttMessageType.unsubscribe Duplicate = false Retain = false Qos = atLeastOnce Size = 0
Message Identifier = 4
Properties = No properties set

Subscription = test/lol

2024-07-09 09:40:18.541591 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
2024-07-09 09:40:18.565973 -- MqttServerConnection::_onData - Message Received Started <<< 
2024-07-09 09:40:18.566007 -- MqttServerConnection::_ondata - adding incoming data, data length is 6, message stream length is 0, message stream position is 0
2024-07-09 09:40:18.566021 -- MqttServerConnection::_ondata - added incoming data message stream length is 6, message stream position is 0
2024-07-09 09:40:18.566030 -- MqttByteBuffer:isMessageAvailable - assumed valid header, value is 176
2024-07-09 09:40:18.567008 -- MqttServerConnection::_onData - MESSAGE RECEIVED -> MQTTMessage of type MqttMessageType.unsubscribeAck
MessageType = MqttMessageType.unsubscribeAck Duplicate = false Retain = false Qos = atMostOnce Size = 4
Message Identifier = 4
Reason String = null
Properties = No properties set

 Reason Code = grantedQos0

2024-07-09 09:40:18.567370 -- MqttServerConnection::_onData - message available event fired
2024-07-09 09:40:18.567382 -- MqttServerConnection::_onData - Message Received Ended <<< 
2024-07-09 09:40:18.567406 -- MqttConnectionHandlerBase::messageAvailable - message type is MqttMessageType.unsubscribeAck
EXAMPLE::Disconnecting
2024-07-09 09:40:20.542782 -- MqttConnectionHandlerBase::disconnect
2024-07-09 09:40:20.543138 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.disconnect
MessageType = MqttMessageType.disconnect Duplicate = false Retain = false Qos = atMostOnce Size = 0

Reason Code  = normalDisconnection
Properties = No properties set
2024-07-09 09:40:20.543805 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
2024-07-09 09:40:20.543854 -- MqttConnectionHandlerBase::_performConnectionDisconnect entered
EXAMPLE::OnDisconnected client callback - Client disconnection
EXAMPLE::OnDisconnected callback is solicited, topic has been notified - this is correct

Process finished with exit code 0

As you can see keepalive is disabled and I'm not seeing any reconnects after 90 seconds.

I get the same result if I set disconnectOnNoResponsePeriod to 5 which is correct this should have no effect if keep alive is disabled.

Can you post your log please and what broker are you using?

GeoffArmstrong commented 1 month ago

In your log, you set the keepAlivePeriod setting to 0. This will not reproduce the problem!

To reproduce the problem, you must either not set the keepAlivePeriod setting or you must set it to the default value of 60.

When you do that, the server will expect keepalive packets every 60 seconds, but the client doesn't send them because the code I posted only starts up the keepalive process if keepAlivePeriod is not the default value of 60 seconds (rather than the correct behavior of only starting up the keepalive process if keepAlivePeriod is not zero).

Here is a log reproducing the problem:

flutter: 2024-07-08 13:53:41.269961: MQTT-INFO MQTT CONNECT
flutter: 2024-07-08 13:53:41.273700 -- Authenticating with username '(removed)' and password '(removed)'
flutter: 2024-07-08 13:53:41.273897 -- MqttClient::checkCredentials - Username length (26) exceeds the max recommended in the MQTT spec.
flutter: 2024-07-08 13:53:41.274707 -- MqttClient::checkCredentials - Password length (15) exceeds the max recommended in the MQTT spec.
flutter: 2024-07-08 13:53:41.276302 -- Mqtt5Client::connect - keep alive is disabled
flutter: 2024-07-08 13:53:41.278347 -- MqttConnectionHandlerBase::connect - server wss://(removed).com/mqtt, port 443
flutter: 2024-07-08 13:53:41.279114 -- MqttSynchronousServerConnectionHandler::internalConnect entered
flutter: 2024-07-08 13:53:41.279323 -- MqttSynchronousServerConnectionHandler::internalConnect - initiating connection try 0, auto reconnect in progress false
flutter: 2024-07-08 13:53:41.279469 -- MqttSynchronousServerConnectionHandler::internalConnect - websocket selected
flutter: 2024-07-08 13:53:41.279956 -- MqttSynchronousServerConnectionHandler::internalConnect - calling connect
flutter: 2024-07-08 13:53:41.280498 -- MqttWsConnection::connectAuto - entered
flutter: 2024-07-08 13:53:41.283834 -- MqttWsConnection:: WS URL is wss://(removed).com:443/mqtt, protocols are [mqtt]
flutter: 2024-07-08 13:53:41.307102: MQTT-INFO MQTT TRY CONNECT: internet connected: true
flutter: 2024-07-08 13:53:41.307483: MQTT-INFO MQTT TRY CONNECT: already connecting
flutter: 2024-07-08 13:53:41.805681 -- MqttServerConnection::_startListening
flutter: 2024-07-08 13:53:41.806782 -- MqttSynchronousServerConnectionHandler::internalConnect - connection complete
flutter: 2024-07-08 13:53:41.806880 -- MqttSynchronousServerConnectionHandler::internalConnect sending connect message
flutter: 2024-07-08 13:53:41.807058 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.connect
MessageType = MqttMessageType.connect Duplicate = false Retain = false Qos = atMostOnce Size = 0
ProtocolName = MQTT
ProtocolVersion = 5
ConnectFlags = CleanStart=true, WillFlag=false, WillQos=atMostOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true
KeepAlive = 60
Properties = No properties setWill topic = null
User name = (removed)
Password = (removed)

Note that KeepAlive is set to 60 but it also says Mqtt5Client::connect - keep alive is disabled.

The server is using the .NET broker at https://www.nuget.org/packages/MQTTnet version 4.3.3.952.

Thank you very much for your work on this project. It's very much appreciated!

shamblett commented 1 month ago

Strangely in your log above you are setting the keepAlive in the connect message to 60, this will enable the broker behaviour you are seeing, i.e. after no control packets from the client for 90 seconds the broker will drop the connection.

If you look at the log provided by me from the example you will see that the keepAlive value in the connect message is zero, this is as it should be, disabling the broker keep alive functionality,

The client is designed to control keep alive itself, i.e. the broker functionality is by default disabled, all it needs to do is respond to ping requests. The setting of the keepAlivePeriod on the client controls only how you want the client to be configured for keep alive, not the broker, the 'keep alive is disabled' message you are seeing in the logs means the client's keep alive functionality is disabled.

If you want to use broker keep alive then you are free to do so but you must then send control packets as needed.

Please refer to the API comment on the keepAlive field of the connect message variable header.

How are you setting this? Presumably you are setting this directly on the connect message.

I agree however that the documentation in and around this area is not clear in what the intended behaviour is, I will update this.

GeoffArmstrong commented 1 month ago

If you set keepAlivePeriod to 60, should it:

As the code currently is, it does the first one, never sends any keepalive packets, due to a bug, as illustrated by the log I posted.

The code is currently if (keepAlivePeriod != 60) { /* send keepalive packets */ }.

When it should be if (keepAlivePeriod != 0) { /* send keepalive packets */ }.

shamblett commented 1 month ago

This won't work, the client code has this -

/// Keep alive period, seconds
  int keepAlivePeriod = MqttConstants.defaultKeepAlive;

The MqttConstants.defaultKeepAlive is set to 60, so in your example above keep alive would always be enabled if the user wanted it or not, if you want to use 0 then you would also have to set MqttConstants.defaultKeepAlive to 0.

This does however allow a value of 0 to be used which is clearly incorrect.

I'll update the client for this.

shamblett commented 1 month ago

Complete, package re released at version 4.4.0.