hivemq / hivemq-mqtt-client

HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
https://hivemq.github.io/hivemq-mqtt-client/
Apache License 2.0
870 stars 161 forks source link

Question. about user properties on automatic reconnect... #640

Open zhangheng1983 opened 3 months ago

zhangheng1983 commented 3 months ago

Checklist

❓ Question

Hi,I set user properties as key is “client_info” to connect packet,when MQTT client connect, server can accept the connect packet with "client_info" properties. But, when client reconnect because of network error etc, the "client_info" properties is absent. Is the user properties cann't be set to reconnect packet?

  1)  In MQTT client code, I bind "client_info" properties as follow:
   final Mqtt5BlockingClient client = Mqtt5Client.builder()
                        .identifier(getClientId())
                        .serverHost(HOST)
                        .serverPort(PORT)
                        .simpleAuth()
                            .username(USERNAME)
                            .password(ByteBuffer.wrap(PASSWORD.getBytes(StandardCharsets.UTF_8)))
                        .applySimpleAuth()
                        .automaticReconnect()
                            .initialDelay(60, TimeUnit.SECONDS)
                            .maxDelay(60, TimeUnit.SECONDS)
                        .applyAutomaticReconnect()
                        .buildBlocking();
   final Mqtt5ConnectBuilder connectBuilder = Mqtt5Connect.builder()
                        .cleanStart(true)
                        // "client_info" properties
                        .userProperties().add("client_info", getClientInfo()).applyUserProperties()
                        .keepAlive(connectKeepAliveInSeconds());

    // That's ok, server can accept the first connect packet with "client_info" properties
    client.connect(connectBuilder.build());
    ......

    2)  In MQTT extensions, I handle the "client_info" properties as follows:
    Services.securityRegistry().setAuthenticatorProvider(input -> new ClientAuthenticator());
    ......
    public class ClientAuthenticator implements SimpleAuthenticator {
        @Override
         public void onConnect(final @NotNull SimpleAuthInput input, final @NotNull SimpleAuthOutput output) {
             // I cann't get "client_info" properties on reconnect
             final List<String> list = input.getConnectPacket().getUserProperties().getAllForName("client_info");
         }
    }

How can I set "client_info" properties on connect package and reconnect packet?

Is it possible to notify "client_info" through publish message?

📎 Additional context

pglombardo commented 2 months ago

Hi @zhangheng1983 - the reasoning for this is also explained here.

To set user properties on reconnect, you can add a addDisconnectedListener and configure the reconnector similar to here. With that you can then call:

connectWith().userProperties.add("client_info", getClientInfo()).applyUserProperties()
pglombardo commented 2 months ago

Hi @zhangheng1983 - have you made any progress on this issue?

dr0-dev commented 2 months ago

what if the reconnection is handled by the automaticReconnect() from the original client? would I need to handle the reconnection manually ?