Friday, January 27, 2017

TLS MQTT broker connection using MqttClient

Last week I was working on a C# application to publish messages using the MqttClinet - https://m2mqtt.wordpress.com/using-mqttclient/

The IBM Watson IoT C# library use the same package :) https://github.com/ibm-watson-iot/iot-csharp

The library is amazing. However a few suggestions from my side:
  1. The documentation is very generic, not explaining the complex scenarios.
  2. The error codes are very generic and the documentation doesn't cover what the return error code number means.
The MQTT broker which we use is configured to use TLS. So, I was looking for a proper documentation to connect to the MQTT using TLS and I couldn't find one. So, I copy pasted the code we used for the connection:

var serverCert = new X509Certificate("certificte path *.cer");

//I  exported the certificate from the server and stored in a project folder.

            _client = new MqttClient("Host name", "port", true, serverCert, null - if you use client certificate, MqttSslProtocols.TLSv1_2);
            if (_client.Connect("Client name", "user name", "password") != 0)
            {
                throw new Exception("Unable to establish connection to MQTT broker");
            }

No comments:

Featured post

How to connect to Mongo Atlas from Robo 3T

If you use a local instance of MongoDB, you might be a great fan of Robo3T. However, if you are using Mongo Atlas, the Atlas web interface p...

Popular Posts