Server address unresolved - cannot connect to rabbitmq server using hivemq client

Hi, I am running a rabbitmq mqtt ws local cluster of servers.
When using paho I connect with tcp://127.0.0.1:8085.
RabbitMQ documentation says I should use ws://127.0.0.1:8085/ws.
When connecting with HiveMQ client (in Java) I apply:

.serverHost(“tcp://localhost”)
.serverPort(8085).

Connection is not established. When debugging the transport config I see that serverAddress is:
tcp://localhost/ < unresolved > :8085

Where and how do I replace the unresolved please? Thank you.

Hi @PeNo,

the tcp:// part is only needed in the Paho client.

This should be the code for the HiveMQ client to connect to a websocket:

final Mqtt5AsyncClient client = Mqtt5Client.builder()
        .serverHost("localhost")
        .serverPort(8085)
        .webSocketConfig(MqttWebSocketConfig.builder().subprotocol("mqtt").serverPath("/ws").build())
        .buildAsync();

client.connect().get();

Greetings,
Michael from the HiveMQ team

1 Like

Hi Michael,

thank you for your reply. But it does not work. I guess first problem might be as per rabbit mq docs:
RabbitMQ supports MQTT 3.1.1 via a plugin that ships in the core distribution. vs suggested client Mqtt5.

As soon as I employ the MqttWebSocketConfig it does not work:

       MqttWebSocketConfig ws = MqttWebSocketConfig.builder()
                .subprotocol("mqtt")
                .serverPath("/ws")
                .build();

        client = Mqtt3Client.builder()
                .automaticReconnectWithDefaultConfig()
                .addConnectedListener(context -> LOG.info("MQTT WS HIVE PUB CONNECTED."))
                .addDisconnectedListener(context -> LOG.warn("MQTT WS HIVE PUB NOT CONNECTED"))

                .identifier("MQTT_HIVE_PUB1")

                .serverHost("localhost")
                .serverPort(8085)
                .webSocketConfig(ws)

                .simpleAuth()
                .username(rabbitConnectionDetail.getUsername())
                .password(rabbitConnectionDetail.getPassword().getBytes(StandardCharsets.UTF_8))
                .applySimpleAuth()

                .buildAsync();

        client.connectWith()
                .cleanSession(true)
                .keepAlive(rabbitConnectionDetail.getKeepAlive())
                .send();

When I use it like below, the connection is created, but I guess this is no ws…

MqttClientTransportConfig transportConfig = MqttClientTransportConfig.builder()
                .serverHost("ws")
                .serverAddress(new InetSocketAddress("localhost", 8085))
                .mqttConnectTimeout(rabbitConnectionDetail.getMqttConnectionTimeout(), TimeUnit.SECONDS)
                .build();

        client = Mqtt3Client.builder()
                .automaticReconnectWithDefaultConfig()
                .addConnectedListener(context -> LOG.info("MQTT WS HIVE PUB CONNECTED."))
                .addDisconnectedListener(context -> LOG.warn("MQTT WS HIVE PUB NOT CONNECTED"))

                .identifier("MQTT_HIVE_PUB1")

                .transportConfig(transportConfig)
                *.THE REST IS IDENTICAL AS ABOVE..*

Any suggestions, please? Thank you…

Hi @michael_w, thank you for your reply. However it does not work. First thing that might make problem is that RabbitMQ currently supports MQTT v 3.1.1. and your suggested client is v5.

I use following code which works (below) - however this comes completely without MqttWebSocketConfig. Can you see any problem with it, please? My RabbitMQ cluster is using MQTT 3.1.1 over WS

    MqttClientTransportConfig transportConfig = MqttClientTransportConfig.builder()
                .serverHost("ws")
                .serverAddress(new InetSocketAddress("localhost", 8085))
                .mqttConnectTimeout(rabbitConnectionDetail.getMqttConnectionTimeout(), TimeUnit.SECONDS)
                .build();

        client = Mqtt3Client.builder()
                .automaticReconnectWithDefaultConfig()
                .addConnectedListener(context -> LOG.info("MQTT WS HIVE PUB CONNECTED."))
                .addDisconnectedListener(context -> LOG.warn("MQTT WS HIVE PUB NOT CONNECTED"))

                .identifier("MQTT_HIVE_PUB1")

                .transportConfig(transportConfig)
                .simpleAuth()
                .username(rabbitConnectionDetail.getUsername())
                .password(rabbitConnectionDetail.getPassword().getBytes(StandardCharsets.UTF_8))
                .applySimpleAuth()

                .buildAsync();

        client.connectWith()
                .cleanSession(true)
                .keepAlive(rabbitConnectionDetail.getKeepAlive())
                .send();

Hi @PeNo,

if you can connect with the standard TCP then the endpoint you are connecting to is most likely not a WebSocket endpoint. I looked up the RabbitMQ documentation they write a plugin needs to be installed in order that MQTT over WebSocket works. Just to be clear you installed that plugin?

Greetings,
Michael

Hi @michael_w , thank you for leading me to fix the issue. Maybe for future reference:
I am using following setup:
grafik
The RabbitMq servers are in Docker containers. Obviously I was exposing external:internal ports in a wrong way like this
8085:1883 for mqtt
Correct is:
8085:80 for mqtt_ws aka http/web-mqtt on the picture.
Now everything works. Thank you.

1 Like