MQTT C Client for Posix and Windows
The Paho MQTT C Client is a fully fledged MQTT client written in ANSI standard C. It avoids C++ in order to be as portable as possible. A C++ layer over this library is also available in Paho.
In fact there are two C APIs. "Synchronous" and "asynchronous" for which the API calls start with MQTTClient and MQTTAsync respectively. The synchronous API is intended to be simpler and more helpful. To this end, some of the calls will block until the operation has completed, which makes programming easier. In contrast, no calls ever block in the asynchronous API. All notifications of API call results are made by callbacks. This makes the API suitable for use in windowed environments like iOS for instance, where the application is not the main thread of control.
Features
Source
Source tarballs are available from the Git repository, as is the source, of course.
Download
Pre-built binaries for Windows, Linux and Mac are available from the downloads page.
The Windows binaries are built with Visual Studio 2013. If you do not have this installed, you will need to install the Visual C++ Redistributable Packages for Visual Studio 2013.
Development builds can also be downloaded here.
Building from source
Linux
The C client is built for Linux/Unix/Mac with make and gcc. To build:
git clone https://github.com/eclipse/paho.mqtt.c.git cd org.eclipse.paho.mqtt.c.git make
To install:
sudo make install
Windows
The Windows build uses Visual Studio or Visual C++. Free Express versions are available. To build:
git clone https://github.com/eclipse/paho.mqtt.c.git cd org.eclipse.paho.mqtt.c.git msbuild "Windows Build\Paho C MQTT APIs.sln" /p:Configuration=Release
To set the path to find msbuild, you can run utility program vcvars32.bat, which is found in a location something like:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin
Documentation
Reference documentation is online here.
Getting Started
These C clients connect to a broker using a TCP/IP connection using Posix or Windows networking, threading and memory allocation calls. They cannot be used with other networking APIs. For that, look at the Embdedded C client.
Here is a simple example of publishing with the C client synchronous API:
#include "stdio.h" #include "stdlib.h" #include "string.h" #include "MQTTClient.h" #define ADDRESS "tcp://localhost:1883" #define CLIENTID "ExampleClientPub" #define TOPIC "MQTT Examples" #define PAYLOAD "Hello World!" #define QOS 1 #define TIMEOUT 10000L int main(int argc, char* argv[]) { MQTTClient client; MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer; MQTTClient_message pubmsg = MQTTClient_message_initializer; MQTTClient_deliveryToken token; int rc; MQTTClient_create(&client, ADDRESS, CLIENTID, MQTTCLIENT_PERSISTENCE_NONE, NULL); conn_opts.keepAliveInterval = 20; conn_opts.cleansession = 1; if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS) { printf("Failed to connect, return code %d\n", rc); exit(-1); } pubmsg.payload = PAYLOAD; pubmsg.payloadlen = strlen(PAYLOAD); pubmsg.qos = QOS; pubmsg.retained = 0; MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token); printf("Waiting for up to %d seconds for publication of %s\n" "on topic %s for client with ClientID: %s\n", (int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID); rc = MQTTClient_waitForCompletion(client, token, TIMEOUT); printf("Message with delivery token %d delivered\n", token); MQTTClient_disconnect(client, 10000); MQTTClient_destroy(&client); return rc; }