Comments (13)
The exception you are getting is raised by the Dart/flutter runtime, not the client so the client can't really do much about this if it occurs.
Not sure why you are null checking the client above, you should be checking the connected state of the client, only if it indicates connected should you call disconnect, the use case here is connect, then either call disconnect or let the client handle an unsolicited disconnect from the network/broker after which you can call connect again. Don't connect while connected or disconnect while disconnected.
I'll need a client log to see exactly what is happening here.
from mqtt5_client.
Whats bothering me, that connection state change happens way late, this is what i have in app.dart
connectionChecker.instance.addNetworkStateChangedCallback((connected) async {
if (connected) {
MqttService().connect();
} else {
MqttService().disconnect();
}
});
Starts when i turn internet off
I/flutter ( 5181): 2024-03-10 16:39:21.374133 -- MqttConnectionBase::_onError - calling disconnected callback
I/flutter ( 5181): 2024-03-10 16:39:21.382589 -- MqttConnectionBase::_onDone - calling disconnected callback
I/flutter ( 5181): 2024-03-10 16:39:21.386489 -- MqttConnectionHandlerBase::autoReconnect entered
I/flutter ( 5181): 2024-03-10 16:39:21.391360 -- MqttConnectionHandlerBase::autoReconnect - attempting reconnection
I/flutter ( 5181): 2024-03-10 16:39:21.391518 -- MqttConnectionHandlerBase::connect - server mqtt.spiriway.com, port 8883
I/flutter ( 5181): 2024-03-10 16:39:21.391544 -- MqttSynchronousServerConnectionHandler::internalConnect entered
I/flutter ( 5181): 2024-03-10 16:39:21.391585 -- MqttSynchronousServerConnectionHandler::internalConnect - initiating connection try 0, auto reconnect in progress true
I/flutter ( 5181): 2024-03-10 16:39:21.391650 -- MqttSynchronousServerConnectionHandler::internalConnect - calling connectAuto
I/flutter ( 5181): 2024-03-10 16:39:21.392874 -- MqttServerSecureConnection::connectAuto - entered
I/flutter ( 5181): 2024-03-10 16:39:21.394311 -- MqttConnectionHandlerBase::autoReconnect entered
I/flutter ( 5181): [2024-03-10 16:39:21.448094 | Catcher 2 | INFO]
I/flutter ( 5181): [2024-03-10 16:39:21.448163 | Catcher 2 | INFO] ---------- ERROR ----------
I/flutter ( 5181): [2024-03-10 16:39:21.448305 | Catcher 2 | INFO] SocketException: Software caused connection abort (OS Error: Software caused connection abort, errno = 103), address = mqtt.spiriway.com, port = 41598
I/flutter ( 5181): [2024-03-10 16:39:21.448335 | Catcher 2 | INFO]
I/flutter ( 5181): [2024-03-10 16:39:21.448501 | Catcher 2 | INFO] ------- STACK TRACE -------
I/flutter ( 5181): [2024-03-10 16:39:21.453617 | Catcher 2 | INFO]
I/flutter ( 5181): [2024-03-10 16:39:21.454244 | Catcher 2 | INFO] ======================================================================
I/flutter ( 5181): [2024-03-10 16:39:21.455208 | Catcher 2 | INFO] Report result: true
I/flutter ( 5181): 2024-03-10 16:39:21.478634 -- MqttSynchronousServerConnectionHandler::internalConnect exception thrown during auto reconnect - ignoring
I/flutter ( 5181): 2024-03-10 16:39:21.478728 -- MqttSynchronousServerConnectionHandler::internalConnect - connection complete
I/flutter ( 5181): 2024-03-10 16:39:21.478749 -- MqttSynchronousServerConnectionHandler::internalConnect sending connect message
I/flutter ( 5181): 2024-03-10 16:39:21.478771 -- MqttConnectionHandlerBase::sendMessage - sending message started >>> -> MQTTMessage of type MqttMessageType.connect
I/flutter ( 5181): MessageType = MqttMessageType.connect Duplicate = false Retain = false Qos = atMostOnce Size = 1507
I/flutter ( 5181): ProtocolName = MQTT
I/flutter ( 5181): ProtocolVersion = 5
I/flutter ( 5181): ConnectFlags = CleanStart=false, WillFlag=false, WillQos=atMostOnce, WillRetain=false, PasswordFlag=true, UserNameFlag=true
I/flutter ( 5181): KeepAlive = 60
I/flutter ( 5181): Properties = Identifier : sessionExpiryInterval, value : 604800
I/flutter ( 5181): Will topic = null
I/flutter ( 5181): User name = user
I/flutter ( 5181): Password = pass
I/flutter ( 5181): 2024-03-10 16:39:21.487814 -- MqttConnectionHandlerBase::sendMessage - sending message ended >>>
I/flutter ( 5181): 2024-03-10 16:39:21.488158 -- MqttSynchronousServerConnectionHandler::internalConnect - pre sleep, state = Connection status is connecting with return code of success and a disconnection origin of none
I/flutter ( 5181): CONNECTION STATE CHANGE TO false
I/flutter ( 5181): 2024-03-10 16:39:21.556953 -- MqttConnectionHandlerBase::disconnect
I/flutter ( 5181): 2024-03-10 16:39:21.558667 -- MqttConnectionHandlerBase::_performConnectionDisconnect entered
I/flutter ( 5181): ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter ( 5181): │ ⚠️ Disconnected from MQTT broker
I/flutter ( 5181): └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
from mqtt5_client.
You seem to have auto reconnect enabled, if you are managing your own connect/disconnect sequence when you transit from online to offline turn this off.
You did however connect and send a connect message the broker then disconnected you before sending a connect ack.
from mqtt5_client.
If i do mqttServerClient.autoReconnect = false;
then when mqtt broker goes down, it would not try to reconnect, is that right?
Edit: implemented custom reconnect logic and it works amazingly, thank you for hints!
from mqtt5_client.
After further testing, on macos it works as intended, but on android (pixel 4 api 33), since its slower/different device, it still throws errors.
I/flutter ( 5899): 2024-03-10 23:38:18.095332 -- MqttConnectionBase::_onError - calling disconnected callback
I/flutter ( 5899): ┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter ( 5899): │ ⚠️ onDisconnected from MQTT broker
I/flutter ( 5899): └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
I/flutter ( 5899): 2024-03-10 23:38:18.122759 -- MqttConnectionBase::_onDone - calling disconnected callback
I/flutter ( 5899): 2024-03-10 23:38:18.123405 -- MqttClient::internalDisconnect - not invoking disconnect, no connection handler
I/flutter ( 5899): [2024-03-10 23:38:18.137733 | Catcher 2 | INFO] ============================== CATCHER 2 LOG ==============================
I/flutter ( 5899): [2024-03-10 23:38:18.137992 | Catcher 2 | INFO] Crash occurred on 2024-03-10 23:38:18.135962
I/flutter ( 5899): [2024-03-10 23:38:18.138043 | Catcher 2 | INFO]
I/flutter ( 5899): [2024-03-10 23:38:18.138082 | Catcher 2 | INFO] ---------- ERROR ----------
I/flutter ( 5899): [2024-03-10 23:38:18.138131 | Catcher 2 | INFO] SocketException: Software caused connection abort (OS Error: Software caused connection abort, errno = 103), address = mqtt.domain.com, port = 35242
I/flutter ( 5899): [2024-03-10 23:38:18.138165 | Catcher 2 | INFO]
I/flutter ( 5899): [2024-03-10 23:38:18.138198 | Catcher 2 | INFO] ------- STACK TRACE -------
I/flutter ( 5899): [2024-03-10 23:38:18.138700 | Catcher 2 | INFO]
I/flutter ( 5899): [2024-03-10 23:38:18.138758 | Catcher 2 | INFO] ======================================================================
I/flutter ( 5899): [2024-03-10 23:38:18.138949 | Catcher 2 | INFO] Report result: true
I/flutter ( 5899): CONNECTION STATE CHANGE TO false
how this can be solved for slower devices?
from mqtt5_client.
The client has no concept of fast or slow devices it just sits atop the Dart/flutter runtime in fact it is unaware what platform it is on so it cant help you with this.
There are many android users, see the closed issues, who are not reporting this, I suggest you maybe reach out to them or even to the flutter guys.
I notice you are using secure sockets, I wonder if different tls implementations could br causing this but again this is not a question for the client.
from mqtt5_client.
Thank you for your explanation, ill try to get this working by raising issue on dart-lang
from mqtt5_client.
Hello, i tried tcp 1883 user pass connection and its still throwing the socket exception, so its not only securesocket.
I tried to setup basic rabbitMQ and use dart_amqp package and that goes without uncaught exception. It shows FatalException, but no crash.
from mqtt5_client.
@shamblett I had a bit of time and i was playing around with the library and i dont have the uncaught exception anymore.
its in mqtt_connection_base.dart
in _disconnect()
function. Ive changed it to this:
void _disconnect() async {
// On disconnect clean(discard) anything in the message stream
messageStream.clean();
if (client != null) {
try {
await client.done;
client.destroy();
client = null;
} catch (error) {
print("error");
}
}
}
since it has done
now with try catch
, i get double print of error, but no uncaught exception. I hope this is a good lead. Could you please make an update so that we can catch it in our code outside the package?
from mqtt5_client.
mqtt_connection_base is a base class for all connection types not just Socket based ones, Socket has a done() method Websockets in dart.io or dart.html don't so this will fail at run time in these environments.
Explain to me again what problem the exception is giving you, could you not just catch it yourself?
Always be mindful of effects on other environments and uers, for instance suddenly changing the client to propagate(or not) an exception may come as a surprise to some users, we don't want this.
from mqtt5_client.
mqtt_connection_base is a base class for all connection types not just Socket based ones, Socket has a done() method Websockets in dart.io or dart.html don't so this will fail at run time in these environments.
for universal_html that could be true, but in websocket i see done prop. https://api.dart.dev/stable/3.3.1/dart-io/WebSocket-class.html so it shows an option to do if client is specific type to do done call.
Explain to me again what problem the exception is giving you, could you not just catch it yourself?
I get connected to MQTT Broker properly. Then turn WIFI OFF on the device (in this testing case Pixel 4 api 33) and when i do that, _onDisconnected() callback is called and then i get UNCAUGHT exception. Since im using Catcher_2 package, when unhandled exception is thrown -> app shows dialog and i get email with this unhandled exception.
I need to catch this exception, But dont know where. Based on this example, can you show me where exactly can i do try catch? https://github.com/shamblett/mqtt5_client/blob/master/example/mqtt5_server_client.dart
Just to sum it up:
- MQTT connected and WIFI on -> everything correct
- MQTT connected and turn WIFI off -> unhandled exception
Always be mindful of effects on other environments and uers, for instance suddenly changing the client to propagate(or not) an exception may come as a surprise to some users, we don't want this.
Every time when there is a new version of any package, i read changelog and breaking changes. 90% of the time its presented in the code as error or warning.
Now since i tried the socket.done and its caught properly, i dont believe its dart:io or dart:async issue. Also when i tested dart_amqp library, that didnt throw unhandled exception, so its handled properly in the library.
from mqtt5_client.
Comparing to amqp lib is of no use it's a server side lib any fix needs to incorporate browser(html) environments.
Other users deal with things like this(WiFi off, going into background etc.) by detecting this and calling disconnect on the client rather than waiting for the network failure to be reflected back to the client. This seems rather lazy, it is not an unsolicited failure, you know it's going to happen
Either way any fix must be for all environments.
You should be able to put a try catch block around your main function somehow also depending on your code you may be able to put the client in it's own zone.
from mqtt5_client.
Other users deal with things like this(WiFi off, going into background etc.) by detecting this and calling disconnect on the client rather than waiting for the network failure to be reflected back to the client. This seems rather lazy, it is not an unsolicited failure, you know it's going to happen
This is what im doing, but network changes after some time, so it happens late.
You should be able to put a try catch block around your main function somehow also depending on your code you may be able to put the client in it's own zone.
I wrapped whole connect function in runZoneGuaded and that worked, so finally this can be closed. Thank you for your ideas.
from mqtt5_client.
Related Issues (20)
- Cannot add new events after calling close HOT 7
- Getting message twice while listening to MqttPublishMessage stream. HOT 2
- Can not receiving client offline messages HOT 1
- Reconnect will calling repeatly when network are poor HOT 8
- OnDisconnect not triggered once connection is established HOT 1
- MqttSubscriptionTopic().matches function throws exception upon different lengths of topics HOT 2
- Fix linter warnings introduced by linter 3.0.0 HOT 1
- Hello, this client everything works fine except it will drop connect by server HOT 4
- Problem to conect Flutterflow an HOT 1
- How to control autoConnect time? HOT 11
- Replace `dart:html` to `universal_html` for better universal support HOT 1
- Null check operator used on a null value HOT 2
- How to recall a time-out message? HOT 4
- MqttBrowserClient set certificate on wss connection HOT 2
- Request/Response full support / Update README HOT 6
- Error on unsubscribeMqttSubscription userProperties is null HOT 3
- how to use MqttBrowserClient HOT 2
- Subject: RangeError in mqtt5_client library - Client-side fix or library update needed? HOT 5
- Would like a way to capture low level socket errors HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mqtt5_client.