I’m working with JWT tokens as passwords. These expire after a while so that on a connect or reconnect you have to provide a valid token.
How to setup my client so that on a reconnect attempt it gets the current username and password?
When creating the client I define a function for gathering the token:
simpleAuth()
.username(mqttConfiguration.getUsername())
.password(getPassword())
.applySimpleAuth()
Welcome to the HiveMQ Community! We’re thrilled to have you with us, especially given your interest in MQTT and the HiveMQ broker. It’s always exciting to see new users join our community.
To better assist you, could you please provide a bit more information?
Which MQTT broker are you using? Is it the HiveMQ Cloud Serverless, HiveMQ Cloud Starter, self-hosted HiveMQ Community Edition, or the self-hosted HiveMQ Enterprise Edition?
Could you share the reconnection logic from your source code? It would be helpful if you could provide the relevant sections of your code or, if your project is in a public GitHub repository, a link to it.
I’m confident that with these details, we can identify the issue together. Thank you, and I look forward to your response!
I found a solution.
You have to call the setter for the password for the reconnector again.
Define a callback on disconnect when you create your client:
.addDisconnectedListener(context → TypeSwitch.when(context).is(Mqtt5ClientDisconnectedContext.class, this::onDisconnect));
In the callback trigger your getUpdatedToken() function:
private void onDisconnect(final Mqtt5ClientDisconnectedContext context) {
…
context.getReconnector()
.connectWith()
.simpleAuth()
.password(getUpdatedToken())