Dedyk Suntoro Atmojo
Dedyk Suntoro Atmojo

Reputation: 37

How to call mqtt client in background service flutter

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

Answers (1)

Manish
Manish

Reputation: 835

Check flutter foreground service it might help you.

Upvotes: 3

Related Questions