Link Search Menu Expand Document Edit GitHub

Disconnect

You can disconnect a MQTT client without the need to provide arguments. This will use the default parameters as defined in the MQTT specification or reasonable defaults if not defined there.

client.disconnect();

The return type depends on the used MQTT version and API flavour.

    • The blocking API for disconnect has a Void return value since disconnects in MQTT are sent only with no response defined.

      client.disconnect();
      
    • The asynchronous API returns a CompletableFuture which completes with a Void return value.

      CompletableFuture<Void> disconnectFuture = client.disconnect();
      
    • The reactive API returns a Single which succeeds with a Void return since no response is expected. As the Single is a reactive type, the following line does not disconnect immediately but only after you subscribe to it (in terms of Reactive Streams).

      Single<Void> disconnectSingle = client.connect();
      
    • The blocking API for disconnect has a Void return value since disconnects in MQTT are sent only with no response defined.

      client.disconnect();
      
    • The asynchronous API returns a CompletableFuture which completes with a Void return value.

      CompletableFuture<Void> disconnectFuture = client.disconnect();
      
    • The reactive API returns a Single which succeeds with a Void return since no response is expected. As the Single is a reactive type, the following line does not disconnect immediately but only after you subscribe to it (in terms of Reactive Streams).

      Single<Void> disconnectSingle = client.connect();
      

  • The rest of this section describes all possible properties of a Mqtt5Disconnect message. They can be set via a fluent builder API.

      • Mqtt5Disconnect disconnectMessage = Mqtt5Disconnect.builder()
                ... // here you can specify multiple properties described below
                .send();
        
      • Mqtt5Disconnect disconnectMessage = Mqtt5Disconnect.builder()
                ... // here you can specify multiple properties described below
                .build();
        
        client.disconnect(disconnectMessage);
        
      • Mqtt5Disconnect disconnectMessage = Mqtt5Disconnect.builder()
                ... // here you can specify multiple properties described below
                .send();
        
      • Mqtt5Disconnect disconnectMessage = Mqtt5Disconnect.builder()
                ... // here you can specify multiple properties described below
                .build();
        
        CompletableFuture<Void> disconnectFuture = client.disconnect(disconnectMessage);
        
      • Mqtt5Disconnect disconnectMessage = Mqtt5Disconnect.builder()
                ... // here you can specify multiple properties described below
                .applyDisconnect();
        
      • Mqtt5Disconnect disconnectMessage = Mqtt5Disconnect.builder()
                ... // here you can specify multiple properties described below
                .build();
        
        Single<Void> disconnectSingle = client.disconnect(disconnectMessage);
        
  • The Disconnect call for MQTT 3.1.1 does not support any properties.


  • Disconnect Properties

    See Also: Mqtt5DisconnectBuilderBase


    Reason Code

    The reason code of a disconnect message indicates to the broker the reason for the disconnect. This can be used to trigger broker behavior.

    PropertyValuesDefaultMQTT Specification
    reasonCodeSee Mqtt5DisconnectReasonCodeNORMAL_DISCONNECTION3.14.2.1
    • client.disconnectWith().reasonCode(Mqtt5DisconnectReasonCode.DISCONNECT_WITH_WILL_MESSAGE)...;
      
    • Mqtt5Disconnect disconnectMessage = Mqtt5Disconnect.builder()
          .reasonCode(Mqtt5DisconnectReasonCode.DISCONNECT_WITH_WILL_MESSAGE)...build();
      

    Session Expiry Interval

    The session expiry interval is the time interval (in seconds) the session will persist when the client is disconnected.

    PropertyValuesDefaultMQTT Specification
    sessionExpiryInterval[0 - 4_294_967_295]N/A3.14.2.2.2

    If the Session Expiry Interval is absent, the Session Expiry Interval in the CONNECT packet is used.

    • client.disconnectWith().sessionExpiryInterval(100)...;
      
    • Mqtt5Disconnect disconnectMessage = Mqtt5Disconnect.builder().sessionExpiryInterval(100)...build();
      

    Session expiry can be disabled by setting it to 4_294_967_295 or using the method noSessionExpiry.

    • client.disconnectWith().noSessionExpiry()...;
      
    • Mqtt5Disconnect disconnectMessage = Mqtt5Disconnect.builder().noSessionExpiry()...build();
      


  • User Properties

    User Properties are user defined name and value pairs which are sent with the Mqtt5Disconnect message.

    MethodValuesMQTT Specification
    userProperties.addString, String
    MqttUtf8String, MqttUtf8String
    Mqtt5UserProperty
    3.1.2.11.8
    • client.disconnectWith()
              .userProperties()
                  .add("name1", "value1")
                  .add(Mqtt5UserProperty.of("name2", "value2"))
                  .applyUserProperties()
              ...
      
    • Mqtt5Disconnect disconnectMessage = Mqtt5Disconnect.builder()
              .userProperties()
                  .add("name1", "value1")
                  .add(Mqtt5UserProperty.of("name2", "value2"))
                  .applyUserProperties()
              ...
              .build();
      

      You can also prebuild the Mqtt5UserProperties.

      Mqtt5UserProperties connectUserProperties = Mqtt5UserProperties.builder()
              .add("name1", "value1")
              .add(Mqtt5UserProperty.of("name2", "value2"))
              .build();
      
      Mqtt5Disconnect disconnectMessage = Mqtt5Disconnect.builder()
              .userProperties(connectUserProperties)
              ...
              .build();