davidepianca98 / KMQTT

Kotlin Multiplatform MQTT client & embeddable and standalone broker
MIT License
188 stars 29 forks source link

connect to activemq #24

Closed AhmedX6 closed 1 year ago

AhmedX6 commented 1 year ago

Hi,

Trying to connect to an activemq instance on aws, Here is my source code:

val client = MQTTClient(
                    5,
                    "id.mq.eu-central-1.amazonaws.com",
                    8883,
                    TLSClientSettings()
                ) {
                    Logger.debug(TAG, "test ${it.payload?.toByteArray()?.decodeToString()}")
                }
                client.publish(
                    false,
                    Qos.AT_MOST_ONCE,
                    "/toto",
                    "hello".encodeToByteArray().toUByteArray()
                )
                client.step()
                client.run()

Exception trapped

socket.SocketClosedException
at socket.tls.TLSSocket.read0(TLSSocket.kt:152)
at socket.tls.TLSSocket.read--5HJl4c(TLSSocket.kt:172)
at TLSClientSocket.read--5HJl4c(TLSClientSocket.kt:117)
at MQTTClient.check(MQTTClient.kt:283)
at MQTTClient.step(MQTTClient.kt:346)
at MQTTClient.run(MQTTClient.kt:355)

Using jitpack version 0.4.1

Please could you test on your side using amazon mq instance on mqtt protocol ?

Thanks

davidepianca98 commented 1 year ago

Please make sure you configured the client appropriately. It seems weird you aren't setting any username and password. Additionally you might have to provide a client certificate and/or the server certificate, depending on the broker's configuration and your target platform. I will try specifically with AWS when I have some time to make sure it's all in order, but as the broker is MQTT compliant it should work just fine.

AhmedX6 commented 1 year ago

I followed the documentation https://activemq.apache.org/how-do-i-use-ssl for creating ssl certificate. Could you provide a sample in Kotlin how to set path on TLSClientSettings data class ?

Where should i put my client/server certs etc.

AhmedX6 commented 1 year ago

Any news ?

davidepianca98 commented 1 year ago

Steps:

  1. ActiveMQ supports only MQTT3.1.1, so you need to set the client MQTT version to 4.
  2. Get the root CA certificate (https://www.amazontrust.com/repository/G2-RootCA1.pem) and save it to a file (like aws.crt).
  3. In the TLSClientSettings constructor you need to pass the path to that file to the serverCertificatePath parameter (for example TLSClientSettings(serverCertificatePath = "aws.crt")).
  4. In your AWS panel go to the Connections section and there will be a link to a guide on how to enable the connection to the broker. You need to allow the specific ports like 8883 in the EC2 security group panel.
  5. Set the userName and password fields in the client constructor to your ActiveMQ username and password you set when creating the instance.

Full sample:

val client = MQTTClient(
        4, // MQTTVersion.MQTT3_1_1 if using the latest version
        "id.mq.us-west-2.amazonaws.com",
        8883,
        TLSClientSettings(serverCertificatePath = "aws.crt"),
        30,
        userName = "testuser",
        password = "testpassword".encodeToByteArray().toUByteArray()
    ) {
        println(it.payload?.toByteArray()?.decodeToString())
    }
AhmedX6 commented 1 year ago

thanks a lot for your answer, so LWT not working ?

davidepianca98 commented 1 year ago

I already replied to your question in this issue https://github.com/davidepianca98/KMQTT/issues/26#issuecomment-1709912159