createNewSubscription method

Subscription? createNewSubscription(
  1. String topic,
  2. MqttQos? qos
)

Creates a new subscription for the specified topic. If the subscription cannot be created null is returned.

Implementation

Subscription? createNewSubscription(String topic, MqttQos? qos) {
  try {
    final subscriptionTopic = SubscriptionTopic(topic);
    // Get an ID that represents the subscription. We will use this
    // same ID for unsubscribe as well.
    final msgId = messageIdentifierDispenser.getNextMessageIdentifier();
    final sub = Subscription();
    sub.topic = subscriptionTopic;
    sub.qos = qos;
    sub.messageIdentifier = msgId;
    sub.createdTime = DateTime.now();
    pendingSubscriptions[sub.messageIdentifier] = sub;
    // Build a subscribe message for the caller and send it off to the broker.
    final msg = MqttSubscribeMessage()
        .withMessageIdentifier(sub.messageIdentifier)
        .toTopic(sub.topic.rawTopic)
        .atQos(sub.qos);
    connectionHandler!.sendMessage(msg);
    return sub;
  } on Exception catch (e) {
    MqttLogger.log('Subscriptionsmanager::createNewSubscription '
        'exception raised, text is $e');
    if (onSubscribeFail != null) {
      onSubscribeFail!(topic);
    }
    return null;
  }
}