Search code examples
javamqtttls1.2paho

Eclipse Paho MQTT client in Java using TLS


I need to create an MQTT client in Java usign the Eclipse Paho.

My MQTT Broker stands behind an Nginx TCP reverse proxy, which has TLS enabled. Therefore, I need to configure the client to use TLS.

Nevertheless, I don't want to provide my client with a certificate (I don't think I really need to, since I can connect with a NodeRED MQTT client with TLS enabled and this client is not provided any certificate; the same - I am told - with a .NET Core client).

Is it possible to do this with Eclipse Paho library? All the examples I can find make use of a certificate.


Solution

  • Yes, you just need to provide ssl:// or mqtts:// URL instead of a tcp:// or mqtt://

    e.g. using the example on Paho Java client page:

    String topic        = "MQTT Examples";
    String content      = "Message from MqttPublishSample";
    int qos             = 2;
    String broker       = "tcp://mqtt.eclipse.org:1883";
    String clientId     = "JavaSample";
    MemoryPersistence persistence = new MemoryPersistence();
    

    You just need to change the broker String to:

    String broker       = "ssl://mqtt.eclipse.org:8883";
    

    This assumes that nginx is listening on port 8883 and that the broker is using a certifacte signed by a public trusted CA.

    All this is also described in the Javadoc here