shamblett / mqtt_client

A server and browser based MQTT client for dart
Other
548 stars 176 forks source link

subscription problem while using autoReconnect = true #170

Closed LaxmikanthMadhyastha closed 4 years ago

LaxmikanthMadhyastha commented 4 years ago

Hi first of all thanks for frequent updates on this package, I tried using autoReconnect = ture while creating the mqttConnection , it works great but faced some problems after reconnection is done. 1) eventhough client reconnects once it is disconnected , onDisconnected call back is not called (im not sure if that is intended) but conected call back is called on successful reconnection 2) im making a persistant connection and on auto reconnect im not subscribing to topics again but still client is making the subscription which we noticed in the broker log and because of that after reconnection i get multiple response for each published message.

any thoughts?

Thanks

shamblett commented 4 years ago

The onDisconnected callback is not called if auto reconnect is specified, the onAutoReconnect callback should be used instead, this is called before auto reconnect commences. This should be in the API docs, if its not clear please tell me and I'll update it.

Poin2 is interesting, I'll have look at that.

LaxmikanthMadhyastha commented 4 years ago

Thank you, i will look into docs again

LaxmikanthMadhyastha commented 4 years ago

to add to this i found one more issue after updating the package to the latest version

So i have subscribed to the network connectivity event of the app so when ever there is a change in connectivity like from wifi to mobile network onDisconnected callback of the mqtt will get called but after updating package that call back is called twice, when i reverted to the previous version it was getting called only once

shamblett commented 4 years ago

If your setting auto reconnect when you create the client you shouldn't be getting onDisconnected called at all, I don't know what you mean by 'network connectivity event of the app', which app? Even without auto reconnect onDisconnected should only be called on disconnect from the broker. Try not setting it on client initialise, i.e. leave it at null and just set onAutoReconnect.

LaxmikanthMadhyastha commented 4 years ago

Im sorry for the confusion created. I meant connectivity event of the phone, so whenever there is a network change the MQTT client behaviour is its onDisconnected will be called. For example when you switch to mobile internet from WiFi or when there is no internet onDisconnected will be called and that time in the updated package onDisconnected is getting called twice. This is happening since i updated the package and also 'not' setting autoReconnect = true ( since i was getting multiple response as i mentioned above i had to discard this for now) and also i cannot set onDisconnected to null since i want to manually reconnect on disconnected.

Thanks.

shamblett commented 4 years ago

If you want to 'manually reconnect on disconnected' you can't use auto reconnect so make sure this is set to off. OK, onDisconnected should not be getting called twice, this is a bug, I've got no specific unit test for this case as it happens, you don't have a log do you?

LaxmikanthMadhyastha commented 4 years ago

Im not sure how usefull the below will be autoReconnect is set false for this log and i have added manual reconnect

I/flutter (23836): Publish Variable Header: TopicName={lschat_6389da605eb85895a2e15601d73b3de9patient}, MessageIdentifier={8537}, VH Length={50}
I/flutter (23836): Payload: {274 bytes={<10><35><97><99><107><48><99><99><54><56><50><50><50><55><102><53><57><52><54><99><54><98><48><49><97><48><50><51><55><50><51><48><53><99><50><101><100><18><3><97><99><107><26><121><10><36><97><52><48><53><57><99><48><55><45><49><101><102><97><45><52><54><98><52><45><98><51><102><51><45><56><54><102><99><52><102><52><51><51><99><51><99><18><7><102><97><105><108><117><114><101><26><66><32><73><110><115><117><102><102><105><99><105><101><110><116><32><100><97><116><97><44><32><80><108><101><97><115><101><32><112><117><115><104><32><97><116><108><101><97><115><116><32><54><48><32><109><105><110><117><116><101><115><32><111><102>
I/flutter (23836): 2020-04-09 22:55:43.087194 -- MqttServerConnection::_onData - message processed
I/flutter (23836): 2020-04-09 22:55:43.087496 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.publishAck
I/flutter (23836): Header: MessageType = MqttMessageType.publishAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (23836): PublishAck Variable Header: MessageIdentifier={8536}
I/flutter (23836): 2020-04-09 22:55:43.087965 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.publishAck
I/flutter (23836): Header: MessageType = MqttMessageType.publishAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (23836): PublishAck Variable Header: MessageIdentifier={8537}
D/DecorView(23836): onWindowFocusChangedFromViewRoot hasFocus: true, DecorView@9d265d8[MainActivity]
I/flutter (23836): 2020-04-09 22:55:53.537240 -- MqttConnectionBase::_onError - calling disconnected callback
I/flutter (23836): true
I/flutter (23836): MQTT client disconnected
I/flutter (23836): true
I/flutter (23836): 2020-04-09 22:55:53.563221 -- MqttConnectionBase::_onDone - calling disconnected callback
I/flutter (23836): true
I/flutter (23836): MQTT client disconnected
I/flutter (23836): true
I/flutter (23836): MQTT Connect called
I/flutter (23836): 2020-04-09 22:55:53.564732 -- MqttConnectPayload::Client id exceeds spec value of 23
I/flutter (23836): MQTT client connecting
I/flutter (23836): 2020-04-09 22:55:53.565370 -- SynchronousMqttServerConnectionHandler::internalConnect entered
I/flutter (23836): 2020-04-09 22:55:53.565491 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0
I/flutter (23836): 2020-04-09 22:55:53.565537 -- SynchronousMqttServerConnectionHandler::internalConnect - insecure TCP selected
I/flutter (23836): MQTT Connect called
I/flutter (23836): 2020-04-09 22:55:53.566585 -- MqttConnectPayload::Client id exceeds spec value of 23
I/flutter (23836): MQTT client connecting
I/flutter (23836): 2020-04-09 22:55:53.567116 -- SynchronousMqttServerConnectionHandler::internalConnect entered
I/flutter (23836): 2020-04-09 22:55:53.567229 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0
I/flutter (23836): 2020-04-09 22:55:53.567313 -- SynchronousMqttServerConnectionHandler::internalConnect - insecure TCP selected
I/flutter (23836): 2020-04-09 22:55:55.196948 -- MqttServerConnection::_startListening
I/flutter (23836): 2020-04-09 22:55:55.197815 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete
I/flutter (23836): 2020-04-09 22:55:55.197984 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message
I/flutter (23836): 2020-04-09 22:55:55.198180 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
I/flutter (23836): Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (23836): Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=false, WillFlag=false, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=60
I/flutter (23836): MqttConnectPayload - client identifier is : mobile_6389da605eb85895a2e15601d73b3de9patient
I/flutter (23836): 2020-04-09 22:55:55.200972 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code noneSpecified
I/flutter (23836): 2020-04-09 22:55:55.511240 -- MqttConnection::_onData
I/flutter (23836): 2020-04-09 22:55:55.511630 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.connectAck
I/flutter (23836): Header: MessageType = MqttMessageType.connectAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 2
I/flutter (23836): Connect Variable Header: TopicNameCompressionResponse={0}, ReturnCode={MqttConnectReturnCode.connectionAccepted}
I/flutter (23836): 2020-04-09 22:55:55.511883 -- MqttServerConnection::_onData - message processed
I/flutter (23836): 2020-04-09 22:55:55.512172 -- SynchronousMqttServerConnectionHandler::_connectAckProcessor
I/flutter (23836): 2020-04-09 22:55:55.512318 -- SynchronousMqttServerConnectionHandler::_connectAckProcessor - state = connected
I/flutter (23836): ERROR: MQTT client connection failed
I/flutter (23836): 2020-04-09 22:56:06.231209 -- SynchronousMqttServerConnectionHandler:: cancelling connect timer
I/flutter (23836): 2020-04-09 22:56:06.232077 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is connected with return code connectionAccepted
I/flutter (23836): 2020-04-09 22:56:06.232319 -- SynchronousMqttServerConnectionHandler::internalConnect exited with state Connection status is connected with return code connectionAccepted
I/flutter (23836): 2020-04-09 22:56:06.239645 -- MqttServerConnection::_startListening
I/flutter (23836): 2020-04-09 22:56:06.240438 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete
I/flutter (23836): 2020-04-09 22:56:06.240670 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message
I/flutter (23836): 2020-04-09 22:56:06.240947 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
I/flutter (23836): Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 0
I/flutter (23836): Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=false, WillFlag=false, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=60
I/flutter (23836): MqttConnectPayload - client identifier is : mobile_6389da605eb85895a2e15601d73b3de9patient
I/flutter (23836): 2020-04-09 22:56:06.242672 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code noneSpecified
I/flutter (23836): 2020-04-09 22:56:11.249128 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is connecting with return code noneSpecified
I/flutter (23836): 2020-04-09 22:56:11.249368 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 1
I/flutter (23836): 2020-04-09 22:56:11.249439 -- SynchronousMqttServerConnectionHandler::internalConnect - insecure TCP selected
I/flutter (23836): 2020-04-09 22:56:19.305299 -- MqttServerConnection::_startListening
I/flutter (23836): 2020-04-09 22:56:19.307169 -- SynchronousMqttServerConnectionHandler::internalConnect - connection complete
I/flutter (23836): 2020-04-09 22:56:19.307994 -- SynchronousMqttServerConnectionHandler::internalConnect sending connect message
I/flutter (23836): 2020-04-09 22:56:19.308314 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.connect
I/flutter (23836): Header: MessageType = MqttMessageType.connect, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 122
I/flutter (23836): Connect Variable Header: ProtocolName=MQIsdp, ProtocolVersion=3, ConnectFlags=Connect Flags: Reserved1=false, CleanStart=false, WillFlag=false, WillQos=MqttQos.atLeastOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true, KeepAlive=60
I/flutter (23836): MqttConnectPayload - client identifier is : mobile_6389da605eb85895a2e15601d73b3de9patient
I/flutter (23836): 2020-04-09 22:56:19.309456 -- SynchronousMqttServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code noneSpecified
I/flutter (23836): 2020-04-09 22:56:21.645962 -- MqttConnection::_onData
I/flutter (23836): 2020-04-09 22:56:21.646421 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.connectAck
I/flutter (23836): Header: MessageType = MqttMessageType.connectAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 2
I/flutter (23836): Connect Variable Header: TopicNameCompressionResponse={0}, ReturnCode={MqttConnectReturnCode.connectionAccepted}
I/flutter (23836): 2020-04-09 22:56:21.646735 -- MqttServerConnection::_onData - message processed
I/flutter (23836): 2020-04-09 22:56:21.647266 -- SynchronousMqttServerConnectionHandler::_connectAckProcessor
I/flutter (23836): 2020-04-09 22:56:21.647500 -- SynchronousMqttServerConnectionHandler::_connectAckProcessor - state = connected
I/flutter (23836): MQTT client connected
I/flutter (23836): Querying task list
I/flutter (23836): 2020-04-09 22:56:26.667448 -- MqttConnectionHandlerBase::sendMessage - MQTTMessage of type MqttMessageType.publish
I/flutter (23836): Header: MessageType = MqttMessageType.publish, Duplicate = false, Retain = false, Qos = MqttQos.atLeastOnce, Size = 0
I/flutter (23836): Publish Variable Header: TopicName={anchat_6389da605eb85895a2e15601d73b3de9patient}, MessageIdentifier={15}, VH Length={0}
I/flutter (23836): Payload: {227 bytes={<10><36><53><101><48><51><102><48><53><54><45><54><50><97><49><45><52><52><56><57><45><98><51><98><52><45><57><48><56><101><50><52><97><52><99><49><97><49><18><5><113><117><101><114><121><26><69><10><13><103><101><116><95><116><97><115><107><95><108><105><115><116><18><9><116><97><115><107><95><108><105><115><116><26><39><54><51><56><57><100><97><54><48><53><101><98><56><53><56><57><53><97><50><101><49><53><54><48><49><100><55><51><98><51><100><101><57><112><97><116><105><101><110><116><56><1><33><0><144><134><168><255><21><119><66><42><46><109><111><98><105><108><101><95><54><51><56><57><100><97><54><48><53><101><98><56><53><56><57><53><
I/flutter (23836): Query task list sent
I/flutter (23836): 2020-04-09 22:56:26.669837 -- SynchronousMqttServerConnectionHandler:: cancelling connect timer
I/flutter (23836): 2020-04-09 22:56:26.670580 -- SynchronousMqttServerConnectionHandler::internalConnect - post sleep, state = Connection status is connected with return code connectionAccepted
I/flutter (23836): 2020-04-09 22:56:26.670874 -- SynchronousMqttServerConnectionHandler::internalConnect exited with state Connection status is connected with return code connectionAccepted
I/flutter (23836): 2020-04-09 22:56:26.687611 -- MqttConnectionBase::_onDone - calling disconnected callback
I/flutter (23836): true
I/flutter (23836): MQTT client disconnected
I/flutter (23836): true
I/flutter (23836): MQTT Connect called
I/flutter (23836): 2020-04-09 22:56:26.689379 -- MqttConnectPayload::Client id exceeds spec value of 23
I/flutter (23836): MQTT client connecting
I/flutter (23836): 2020-04-09 22:56:26.690395 -- SynchronousMqttServerConnectionHandler::internalConnect entered
I/flutter (23836): 2020-04-09 22:56:26.690567 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0
I/flutter (23836): 2020-04-09 22:56:26.690684 -- SynchronousMqttServerConnectionHandler::internalConnect - insecure TCP selected
I/flutter (23836): 2020-04-09 22:56:28.056999 -- MqttConnectionBase::_onDone - calling disconnected callback
I/flutter (23836): true
I/flutter (23836): MQTT client disconnected
I/flutter (23836): true
I/flutter (23836): MQTT Connect called
I/flutter (23836): 2020-04-09 22:56:28.064291 -- MqttConnectPayload::Client id exceeds spec value of 23
I/flutter (23836): MQTT client connecting
I/flutter (23836): 2020-04-09 22:56:28.065433 -- SynchronousMqttServerConnectionHandler::internalConnect entered
I/flutter (23836): 2020-04-09 22:56:28.065684 -- SynchronousMqttServerConnectionHandler::internalConnect - initiating connection try 0
I/flutter (23836): 2020-04-09 22:56:28.065865 -- SynchronousMqttServerConnectionHandler::internalConnect - insecure TCP selected
I/flutter (23836): 2020-04-09 22:56:28.069263 -- MqttConnection::_onData
I/flutter (23836): 2020-04-09 22:56:28.069703 -- MqttServerConnection::_onData - message received MQTTMessage of type MqttMessageType.connectAck
I/flutter (23836): Header: MessageType = MqttMessageType.connectAck, Duplicate = false, Retain = false, Qos = MqttQos.atMostOnce, Size = 2
I/flutter (23836): Connect Variable Header: TopicNameCompressionResponse={0}, ReturnCode={MqttConnectReturnCode.connectionAccepted}
shamblett commented 4 years ago

OK, you disconnect here

2020-04-09 22:56:26.687611 -- MqttConnectionBase::_onDone - calling disconnected callback

then again here

22:56:28.056999 -- MqttConnectionBase::_onDone - calling disconnected callback

after calling connect, it looks as though when you fail to connect you get an on done hence onDisconnect is called, this looks like correct behaviour, onDisconnect is being called when the client disconnects.

Ok, I think what you need to do is when you first enter the disconnect callback set onDisconnect to null, so it can't be called again from any subsequent connect operations, then when you connect and the connect callback is invoked re-set it onDisconnected to your callabck. You're callbacks are becoming nested. You will have to check for correct connection in your disconnect callback and retry as needed as now of course you wont re enter the disconnect callback.

Excepting point 2 above why can't you use auto reconnect for this and just call the API method from your disconnect callback?

LaxmikanthMadhyastha commented 4 years ago

Okay seems to be problem from my side, i will verify again and come back

I did try using autorReconnect = true and i was getting multiple responses after the reconnection is done. Now i will try using onAutoReconnect callback and see

Thanks a lot

zxl777 commented 4 years ago

Change .startClean() to // .startClean()

subscription problem would be fixed.

xhidnoda commented 4 years ago

@LaxmikanthMadhyastha Hey!!

Can you help me? i face the same problem (i recibe duplicate response after reconnet)

Please!

jahnli commented 2 years ago

Okay seems to be problem from my side, i will verify again and come back

I did try using autorReconnect = true and i was getting multiple responses after the reconnection is done. Now i will try using onAutoReconnect callback and see

Thanks a lot

How to solve the problem of automatic reconnection will get repeated response