My search for a clear-cut description about getting started with MQTT, C++ and an SSL option was unsuccessful. This contribution aims to facilitate similar efforts of the peer developers.
The article starts by describing the setup of the server, the acquisition, and compilation of the client libraries and concludes with a simple demo application.
I work in a Windows 7 64 bit Visual Studio 2017 environment.
MQTT server setup
Let’s first tackle the MQTT server. It is ordinarily also called the broker. There are different products around. We went for the EMQ.
When first installed to the C:\Program Files\emqttd folder, the following error message showed up: “Node undefined not responding to pings”.
So, I decided to install it to the C root folder in order to circumvent the space in the path.
Start the server and voilà, it shows up under http://localhost:18083.
Acquisition and compilation of the MQTT client libraries
Because we want to incorporate MQTT into our C++ source code, we need the client libraries. I found, there are basically two variants: Mosquitto and Paho. Because the installation of mosquitto as a broker yielded missing DLL errors, I decided to go for Paho. Paho itself consists of a C and a C++ library. For SLL, openssl is required.
openssl
I wanted to spare myself the hassle of building the library from the source files and fixing all issues. Binary versions can be obtained here.
This library was also installed to the C root folder.
Paho C library
Get the Paho C library from Github.
Use CMake in order to generate a Visual Studio solution file. This description helped me get along.
Configure – Generate – Open project and build the solution.
Paho C++ library
Get he Paho C++ library from Github.
Use CMake again and build the resulting solution.
Demo
The demo will utilize the Paho C++ samples found in the src\samples subfolder. In order to show the MQTT publish/subscribe mechanism with SSL and because we want to have a non-blocking approach, I chose the async_subscribe.cpp and the ssl_publish.cpp files as a base for two new Visual Studio solution files.
Prepare async subscribe
- Create a new empty project and add the async_subscribe.cpp file.
- Update the project settings as shown below
- Copy the following DLL files to the output folder (eg. Debug): libcrypto-1_1.dll, libssl-1_1.dll, paho-mqtt3as.dll
- Build the solution
Update the include directories so that the header files can be found.
Because of the empty project created, do not forget the preprocessor definitions. Alternatively, go for property sheets.
When it comes to the linker, we need to point to the directory for additional libraries.
And we must add them.
Prepare ssl publish
- Create a new empty project and add the ssl_publish.cpp file.
- Update the project settings, copy the DLL files and build the solution as shown above
- Copy the test-root-ca.crt file to the output directory
- Update the server port to 8883 for the EMQ broker
- Update the connection options to omit the user name and password like here
//const std::string DFLT_SERVER_ADDRESS{ "ssl://localhost:18885" }; const std::string DFLT_SERVER_ADDRESS{ "ssl://localhost:8883" };
mqtt::connect_options connopts;// ("testuser", "testpassword");
Run the demo, first try
- Ensure the broker is running (see above)
- In order that the publisher’s messages are not lost first start the async_subscribe sample
- In the ssl_publish sample set a breakpoint before the connection is cut. Otherwise, the program immediately exits:
conntok = client.disconnect();
Unfortunately, the following line in the ssl_publish sample returns with exit code 1.
conntok->wait();
Run the demo
With the server certificate missing in this basic setup its validation needs to be turned off for now.
mqtt::ssl_options sslopts; sslopts.set_trust_store("test-root-ca.crt"); sslopts.set_enable_server_cert_auth(false);
The listeners view shows the two clients on the different ports.
Next, we see the console output of the publisher.
And the output of the subscriber. Because the topic of both samples is the same, the subscriber shows the published messages here.
This should serve as a starting point. Further reading is highly recommended. I found the following blog series helpful.
Further reading:
- Part 1: Introduction
- Part 2: Publish & Subscribe
- Part 3: Client, Broker and Connection Establishment
- Part 4: Publish, Subscribe & Unsubscribe
- Part 5: Topics & Best Practices
- Part 6: Quality of Service 0, 1 & 2
- Part 7: Persistent Session and Queuing Messages
- Part 8: Retained Messages
- Part 9: Last Will and Testament
- Part 10: Keep Alive and Client Take-Over