Skip to content
Advertisement

Java Paho MQTT client connection over reverse proxy

I’m using the Java paho library to communicate to an mqtt broker. Using the code below I’m able to connect fine.

MqttClient publisher = new MqttClient("tcp://192.168.1.100:1883","randomClientId");
MqttConnectOptions options = new MqttConnectOptions();
options.setAutomaticReconnect(true);
options.setCleanSession(true);
options.setConnectionTimeout(10);
publisher.connect(options);

However, I have the mqtt broker behind a reverse proxy, so I don’t need to open up a separate port. So what I need to do is instead of connecting to “tcp://192.168.1.100:1883” I’d like to connect to “tcp://192.168.1.100/mqtt”. However, when I try this, I get an error as below:

Exception in thread "main" java.lang.IllegalArgumentException: URI path must be empty "tcp://13.251.5.125/mqtt"

I can do this just fine using libraries in python for example, but using the Java client I’m not sure how to do so.

Advertisement

Answer

That’s not possible to do with native MQTT.

The only way to get this to work is if you are using MQTT over Websockets. In which case you need to pass a URI that looks like:

ws://192.168.1.100/mqtt

Note it now starts with ws:// not tcp://

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement