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
860 stars 159 forks source link

Ability to update auth credentials in onDisconnected event #534

Closed oreillymj closed 7 months ago

oreillymj commented 2 years ago

I added this as a comment to https://github.com/hivemq/hivemq-mqtt-client/issues/346, but since it's closed, I'll open a specific issue.

I have a scenario where I use a local MQTT server while my device is on my local LAN, but when I lose connection I switch to a Cloud broker.

So in the onDisconnected() even handler I can switch between the local server, but how can I apply different auth per server?

if (localserver){ context.getReconnector().transportConfig().serverHost(localhost).serverPort(localport).applyTransportConfig(); }else{ context.getReconnector().transportConfig().serverHost(cloudhost).serverPort(cloudport).applyTransportConfig(); //need to change the username and password here }

This issue https://github.com/hivemq/hivemq-mqtt-client/issues/344 looks like it might help me, but unfortunately didn't show the implementation of setting the auth parameters outside of a MqttClientBuilder.

SgtSilvio commented 2 years ago

Hi @oreillymj You can set credentials via the reconnector, something like so:

Mqtt5Client.builder()
        .serverHost(localhost)
        .addDisconnectedListener(context -> {
            if (localserver) {
                context.getReconnector().transportConfig().serverHost(nextHost).applyTransportConfig();
                // connect properties can be specific to the MQTT version so we need to switch here
                TypeSwitch.when(context)
                        .is(Mqtt3ClientDisconnectedContext.class, context3 -> {
                            context3.getReconnector()
                                    .connectWith()
                                    .simpleAuth().username("local").password("localpw".getBytes()).applySimpleAuth()
                                    .applyConnect();
                        })
                        .is(Mqtt5ClientDisconnectedContext.class, context5 -> {
                            context5.getReconnector()
                                    .connectWith()
                                    .simpleAuth().username("local").password("localpw".getBytes()).applySimpleAuth()
                                    .applyConnect();
                        });
            } else {
                ...
            }
        })
        ...
        .build();

I hope this helps you. Feel free to comment below.

michaeloreillyintel commented 2 years ago

Thank you for the update. This does appear to switch servers with credentials, but I see that the client variable I've defined doesn't appear to synch those changes. The code below always shows the host and port it was set to initially.

MqttClientState state = client.getState(); Log.i(TAG, "onConnect->Client connected = " + state.isConnected()); Mqtt3ClientConfig config= client.getConfig(); Log.i(TAG, "onConnect->Client connected to = " + config.getServerHost()); Log.i(TAG, "onConnect->Client connected to = " + config.getServerPort());

pglombardo commented 7 months ago

Hi @michaeloreillyintel - since this issue has gone stale, could you let us know if this is still an issue for you?

pglombardo commented 7 months ago

I'll close this issue out but if anything remains feel free to add a comment or file another issue.