unsubscribe method

Future unsubscribe(
  1. String topic,
  2. IMqttMessageListener listener
)

Implementation

Future unsubscribe(String topic, IMqttMessageListener listener) async {
  // Find the subscription index
  var index = subscriptions_
      .indexWhere((s) => s.topic == topic && s.listener == listener);
  if (index < 0) {
    return;
  }

  // Remove the subscription
  subscriptions_.removeAt(index);

  // Check if there other subscriptions to the same topic
  index = subscriptions_.indexWhere((s) => s.topic == topic);

  // Unsubscribe from topic if connection is still open
  if (connection_ != null && index < 0) {
    await Future(() {
      connection_!.unsubscribe(topic);
    });
  }
}