Closed lucasjinreal closed 6 months ago
If you are using the autoreconnect functionality it will run till it reconnects unless you stop it.
I cant comment on why this affects your ui.
shall make connect function be a async function?
Currently it happens when network are not stable, don't why
If your network is so unstable you can't connect then you ate going to have trouble with MQTT. It makes no difference to the client whether its sync or async, if you can't connect you are stuck. You need to put the client as a whole on its own isolate maybe.
Is there any reconnect timeout try times or interval time params to set? Currently it will reonnect quickly when network unstable (note: not offline, if offline I can detect, but network not stable or slow I can not really detect this)
You can change the amount of retries, see the API spec. You could monitor the connect callback, if this is not called after a time interval you could disconnect, back off and try connecting later.
@shamblett thanks.
I have set maxReconnectTimes to 3:
_client =
MqttServerClient.withPort(host, cid, 1883, maxConnectionAttempts: 3);
// MqttServerClient.withPort("test.mosquitto.org", cid, 1883,
// maxConnectionAttempts: 3);
kLog(
'!!!! ------------> [conn] connect with $host, $port, $username, $password, ${_clientId} $_client');
// _client!.logging(on: true);
_client!.logging(on: false);
_client!.onConnected = onConnected;
_client!.onDisconnected = onDisconnected;
_client!.onUnsubscribed = onUnsubscribed;
_client!.onSubscribed = onSubscribed;
_client!.onSubscribeFail = onSubscribeFail;
_client!.onAutoReconnect = onAutoReconnect;
_client!.pongCallback = pong;
_client!.keepAlivePeriod = 60 * 3;
_client!.autoReconnect = true;
_client!.useWebSocket = useWebsocket;
final connMessage = MqttConnectMessage()
.authenticateAs(username, password)
.withSessionExpiryInterval(3600 * 24 * 14) // last for 7 days
.keepAliveFor(120)
// .withWillTopic('lastwills')
// .withWillMessage('Will message')
.withClientIdentifier(cid);
// .withProtocolName("MQTT")
// .withProtocolVersion(4)
// .withSessionExpiryInterval(interval)
// .withWillQos(MqttQos.atLeastOnce);
_client!.connectionMessage = connMessage;
But, when the network are poor, the connect actually connects, but it still will reconnect, ( i have receive the connected welcome message).
Can u give me some suggestions, why I set max tries and reconnect, it will reconnect repeatly very quickly?
Your using autoreconnect = true so post initial connection.if the connection is lost the client will try and reconnect automatically forever.
If you turn this off you can control when you reconnect after a disconnect using the onConnected/onDisconnected callbaclks.
Your using autoreconnect = true so post initial connection.if the connection is lost the client will try and reconnect automatically forever.
If you turn this off you can control when you reconnect after a disconnect using the onConnected/onDisconnected callbaclks.
thanks, how to turn it off? You mean do not auto reconnect? But I need auto reconnect, in case people offline it will auto reconnect,
but the problem is the reconnect interval are too quicky
hi, I found when the network are poor, the mqtt reconnect will run repeatly fast, caused whole UI stoke, why