Search code examples
dockermqtthivemqmqtt.js

Connecting mqtt.js client to local hivemq broker running in docker fails


I'm trying to establish a connection from my angular application via mqtt.js client to my local hivemq broker running on docker with the following connection details:

const options: IClientOptions = {
  host: 'mqtt://0.0.0.0',
  port: 1883,
  clientId: 'myclient',
  username: 'myuser',
  password: '',
  path: "/mqtt"
};
this.client = mqtt.connect(options);

Connecting fails as in the console is logged: "WebSocket connection to 'ws://localhost:1883/mqtt' failed:" but with no reason why.

Docker runs:

MVy2P: no members discovered after 2001 ms: creating cluster as first member
No user for HiveMQ Control Center configured. Starting with default user
Starting HiveMQ Control Center on address 0.0.0.0 and port 8080
Control Center Audit Logging started.
Started HiveMQ Control Center in 203ms
Starting TCP listener on address 0.0.0.0 and port 1883
Starting Websocket listener on address 0.0.0.0 and port 8000
Started TCP Listener on address 0.0.0.0 and on port 1883.
Started Websocket Listener on address 0.0.0.0 and on port 8000.
Started HiveMQ in 8108ms

Using hivemq client I can establish a connection in my backend (Java/Spring). So only the frontend client fails.


Solution

  • 0.0.0.0 is a shortcut to tell a server to listen on all available interfaces, it is meaningless to use it as a address a client should connect to.

    Assuming the container has mapped it's ports to the host machine you should be able to use the address localhost for a client running on the host machine. If you are trying to connect from another container, then you will either need to use the external IP of the host machine, the host machine's IP address on the docker bridge or the IP address of the broker container