shamblett / mqtt5_client

A server and browser based MQTT 5 client for dart
Other
49 stars 25 forks source link

Can not receiving client offline messages #74

Closed lucasjinreal closed 6 months ago

lucasjinreal commented 9 months ago

Hello, I have connect to server like this:

 _client!.logging(on: false);
      _client!.onConnected = onConnected;
      _client!.onDisconnected = onDisconnected;
      _client!.onUnsubscribed = onUnsubscribed;
      _client!.onSubscribed = onSubscribed;
      _client!.onSubscribeFail = onSubscribeFail;
      _client!.onAutoReconnect = onAutoReconnect;
      _client!.pongCallback = pong;
      _client!.keepAlivePeriod = 60 * 3;
      _client!.autoReconnect = false;
      _client!.useWebSocket = useWebsocket;

      final connMessage = MqttConnectMessage()
          .authenticateAs(username, password)
          // .withWillTopic('lastwills')
          // .withWillMessage('Will message')
          .withClientIdentifier(cid);
      // .withProtocolName("MQTT")
      // .withProtocolVersion(4)
      // .withSessionExpiryInterval(interval)
      // .withWillQos(MqttQos.atLeastOnce);
      _client!.connectionMessage = connMessage;

But it can not receive offline messages, all messages which send to client when it was offline all missed.

Why?

shamblett commented 9 months ago

You need to start a session with the broker, look at this API -

/// Starts a persistent session with the broker.
  /// The [sessionExpiryInterval] can be any none zero value up to the the maximum
  /// [MqttConnectVariableHeader.sessionDoesNotExpire].
  /// If 0 is passed the maximum value is used.
  /// Mutually exclusive with [startClean], the last method applied to the message will take
  /// effect.
  MqttConnectMessage startSession(

and read the MQTT5 spec for more details.