Reputation: 37
My project uses mqtt for notifications. And need real time notification. I have mqtt class like this
class Mqtt {
...
Mqtt._() {
mqttClient = MqttServerClient.withPort(server, clientIdentifier, port);
///Successful connection callback
mqttClient.onConnected = _onConnected;
///Disconnected callback
mqttClient.onDisconnected = _onDisconnected;
///Subscription success callback
mqttClient.onSubscribed = _onSubscribed;
///Subscription failure callback
mqttClient.onSubscribeFail = _onSubscribeFail;
}
static Mqtt getInstance() {
if (_instance == null) {
_instance = Mqtt._();
}
return _instance!;
}
///connection
connect() {
mqttClient.connect();
_log("connecting");
}
...
}
And I call like this
Mqtt mqtt = Mqtt.getInstance();
mqtt.subTopic = 'NOTIFICATION/';
mqtt.connect();
But mqtt disconnects when app is stopped. How to call mqtt client in background service flutter?
Upvotes: 3
Views: 1617