Skip to content

How do you configure MQTTClient with userName and password? #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
DieterHolz opened this issue Feb 27, 2025 · 3 comments
Open

How do you configure MQTTClient with userName and password? #82

DieterHolz opened this issue Feb 27, 2025 · 3 comments

Comments

@DieterHolz
Copy link

I try to connect to a MQTT Broker with username/password authentication but I can't get the MQTTClient to connect.

Calling client.run() throws

Exception in thread "main" io.github.davidepianca98.socket.IOException: Connection reset
at io.github.davidepianca98.socket.tcp.Socket.readToBuffer(Socket.kt:70)
at io.github.davidepianca98.socket.tcp.Socket.read--5HJl4c(Socket.kt:75)
at io.github.davidepianca98.ClientSocket.read--5HJl4c(ClientSocket.kt:40)
at io.github.davidepianca98.MQTTClient.check(MQTTClient.kt:389)
at io.github.davidepianca98.MQTTClient.step(MQTTClient.kt:466)
at io.github.davidepianca98.MQTTClient.run(MQTTClient.kt:476)

@DieterHolz
Copy link
Author

DieterHolz commented Mar 2, 2025

With HiveMQ MQTT Client I get a connection by doing this:

client = Mqtt5Client.builder()
.serverHost(mqttBroker)
.identifier(UUID.randomUUID().toString())
.sslWithDefaultConfig()
.simpleAuth()
.username(username)
.password(UTF_8.encode(password))
.applySimpleAuth()
.serverPort(8883)
.buildAsync()

With your library I tried the following, but this fails

val client = MQTTClient(
    MQTTVersion.MQTT5,
    mqttBroker,
    8883,
    null,
    userName = username,
    password = password.encodeToByteArray().toUByteArray(),
    onConnected = {
        println("connected")

    }
) {
    println(it.topicName)
    println(it.payload?.toByteArray()?.decodeToString())
}

@Uplyuz
Copy link

Uplyuz commented Mar 20, 2025

We are also trying to connect to HiveMQ using this library, any updates on this issue?

@davidepianca98
Copy link
Owner

Hello, it looks like you are setting the TLSSettings to null and port 8883 which usually is used for TLS connections unless configured differently. Can you please take a look and make sure you are providing the required TLS parameters to connect? You can find a simple example here https://github.com/davidepianca98/KMQTT?tab=readme-ov-file#tls-code-example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants