HiveMQ

(SOLVED) How to set HiveMQ broker to connect using mqtt:// instead of ws://

Hi,

Could you please provide configuration information to connect to HiveMQ broker using “mqtt://” protocol instead of "ws://.

Regards,
Harsha

Hi @harshabuggi,

You can find the description on how to use Websockets with HiveMQ in our documentation.

<hivemq xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

   <listeners>
      <websocket-listener>
          <port>8000</port>
          <bind-address>0.0.0.0</bind-address>
          <path>/mqtt</path>
          <name>my-websocket-listener</name>
          <subprotocols>
              <subprotocol>mqttv3.1</subprotocol>
              <subprotocol>mqtt</subprotocol>
          </subprotocols>
          <allow-extensions>true</allow-extensions>
      </websocket-listener>
       ...
   </listeners>
    ...
</hivemq>

Kind regards,
Florian

Hi @fraschbi,

Thank you for your response. My websocket listener is configured like this

< websocket-listener >
< port >${PORT}< /por t>
< bind-address >0.0.0.0< /bind-address >
< path >/mqtt< /path >
< name >my-websocket-listener< /name >
< subprotocols >
< subprotocol >mqttv3.1< /subprotocol >
< subprotocol >mqtt< /subprotocol >
< /subprotocols >
< allow-extensions >true< /allow-extensions >
< /websocket-listener >

But I am not able to connect to the broker using connection string

“mqtt://< hostname >:< portnumber >/mqtt”

Regards,
Harsha

You need to use ws:// instead of mqtt:// when connecting over websocket.
It’s not possible to use MQTT when connecting over a browser.
See this blog post

Regards,
Florian

Hi @fraschbi,

I need to connect using a NodeJS mqtt client and not with a browser. How can I modify the config.xml to enable connecting with “mqtt://”

i was able to deploy a cluster using HiveMQ cloud and access using “mqtts://”. But the hostname is too long and I cannot use it for my application. So I am trying to find the configuration to enable this in HiveMQ docker container.

Appreciate your help.

Thank you.

Regards,
Harsha

Hi @harshabuggi,

Sorry about the confusion then.
Simply use a TCP listener then.

<?xml version="1.0"?>
<hivemq xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

    ...
    <listeners>
        ...
        <tcp-listener>
            <port>1883</port>
            <bind-address>0.0.0.0</bind-address>
            <name>my-tcp-listener</name>
        </tcp-listener>
    </listeners>
    ...
</hivemq>

You can try to connect to the HiveMQ public broker for testing.

mqtt://broker.hivemq.com:1883 is likely the correct URL for your application.

Best,
Florian

Hi @fraschbi,

I currently cannot use a public broker. I am not able to connect to the tcp port using the connection string

“mqtt//:< my hostname >:< port >”

Getting response “connect ECONNREFUSED 127.0.0.1:7883” (7883 is the port I have set for tcp listener).

Please advice.

Regards,
Harsha

Maybe this blog can help: Using the Node.js MQTT Client-Starting Guide
Or the official documentation: GitHub - mqttjs/MQTT.js: The MQTT client for Node.js and the browser

Kind regards,
Florian

HI @fraschbi,

This is working. I had not started the container with the right port mapping.

Thank you so much.

Regards,
Harsha

1 Like