HiveMQ Throws Exception When Connecting

I’m having wierd problems connecting to a MQTT broker using HiveMQ Async client. I’m having an unit test that simpley checks publishing and veriying the published message via a Subscriber. Here is the snippet:

“MqttClientFactory#publish” should “connect to a local MQTT broker and publish” in {
val mqttConfig = MqttConfig(“cpo-platform-test”, “localhost”, 1883, “joesan”, “joesan”)
val published = MqttClientFactory.publish(
mqttConfig,
“cpo-test-topic”,
MqttQos.EXACTLY_ONCE
)
whenReady(published, timeout(Span(100, Seconds))) { Unit => {
val client = MqttClientFactory.asyncMqttClient(mqttConfig)
println("In here ****************** ")
client
.connect()
.thenCompose(_ => client.subscribeWith().topicFilter(“cpo-test-topic”).qos(MqttQos.EXACTLY_ONCE).callback(println).send())
}
}
}

When I ran this, I get the following error:

The future returned an exception of type: java.util.concurrent.CompletionException, with message: com.hivemq.client.mqtt.exceptions.MqttClientStateException: MQTT client is not connected…
ScalaTestFailureLocation: com.openelectrons.cpo.mqtt.MqttClientFactoryTest at (MqttClientFactoryTest.scala:28)
org.scalatest.exceptions.TestFailedException: The future returned an exception of type: java.util.concurrent.CompletionException, with message: com.hivemq.client.mqtt.exceptions.MqttClientStateException: MQTT client is not connected…
at org.scalatest.concurrent.Futures$FutureConcept.tryTryAgain$1(Futures.scala:531)
at org.scalatest.concurrent.Futures$FutureConcept.futureValueImpl(Futures.scala:550)
at org.scalatest.concurrent.Futures$FutureConcept.futureValueImpl$(Futures.scala:479)

What could be the issue? The mqtt broker is indeed running on port 1883, here is s screenshot of the same:

Hello @Joesan ,

Thank you for the outreach here and the code snippet!

One area that I might recommend initially is first testing the configuration with a testing tool, such as the HiveMQ MQTT CLI tool, to attempt a connection with the configuration you are looking to use here - i.e., the username, password, and port. More information on the MQTT CLI Tool, and how to use it for testing can be found here.

As an addendum, there are typically special considerations that are required when using QoS 2 messaging - the “EXACTLY_ONCE” specified with the Mqtt.Qos property. The exact differences between QoS levels can be found here in our Essentials series. I’d like to see if these connection failures are returned when adjusting this QoS to QoS 1 or 0. It does look like, based on the provided error, that we are seeing a failure on the connection process, so I’d like to test with the CLI tool first, if possible.

Let us know your findings, or if there is any additional support we can offer at this time!

Best,
Aaron from the HiveMQ Team