• Stars
    star
    976
  • Rank 46,912 (Top 1.0 %)
  • Language
    C++
  • License
    Other
  • Created almost 9 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Eclipse Paho MQTT C++ Client Library

Build Status

This repository contains the source code for the Eclipse Paho MQTT C++ client library on memory-managed operating systems such as Linux/Posix and Windows.

This code builds a library which enables C++11 applications to connect to an MQTT broker, publish messages to the broker, and to subscribe to topics and receive published messages.

The library has the following features:

  • Support for MQTT v3.1, v3.1.1, and v5.
  • Network Transports:
    • Standard TCP
    • Secure sockets with SSL/TLS
    • WebSockets
      • Secure and insecure
      • Proxy support
  • Message persistence
    • User configurable
    • Built-in File persistence
    • User-defined key/value persistence easy to implement
  • Automatic Reconnect
  • Offline Buffering
  • High Availability
  • Blocking and non-blocking API's
  • Modern C++ interface (C++11 and better)

This code requires the Paho C library by Ian Craggs, et al., specifically version 1.3.8 or possibly later.

Latest News

To keep up with the latest announcements for this project, or to ask questions:

Twitter: @eclipsepaho and @fmpagliughi

EMail: Eclipse Paho Mailing List

Mattermost: Eclipse Mattermost Paho Channel

Unreleased features in this branch

  • Added Session Expiry Interval to v5 chat sample
  • Minor tweaks to prepare for C++20
  • Minor cleanup of the tests #317 String constructor using just len instead of end iterator. #337 Copy and move constructors/assignment of ssl_options not handling CA path.

What's new in Version 1.2.0

This release brings in some missing MQTT v5 features, support for websocket headers and proxies, ALPN protocol lists, adds the builder pattern for options, and fixes a number of bugs in both the C++ library and the underlying C lib.

Requires Paho C v1.3.8

  • Missing MQTT v5 features:
    • Ability to add properties to Subscribe and Unsubscribe packets (i.e. subscription identifiers)
    • "Disconnected" callback gives reason code and properties for server disconnect
  • New create_options that can be used to construct a client with new features:
    • Send while disconnected before the 1st successful connection
    • Output buffer can delete oldest messages when full
    • Can choose to clear the persistence store on startup
    • Select whether to persist QoS 0 messages
  • Started classes to create options using the Builder Pattern, with the create_options_builder, connect_options_builder, message_ptr_builder, etc.
  • User-defined websocket HTTP headers.
  • HTTP/S proxy support
  • Added ALPN protocol support to SSL/TLS options
  • SSL/TLS error and PSK callback support
  • Update connection callback support (change credentials when using auto-reconnect)
  • Updates to the sample apps:
    • Overall cleanup with better consistency
    • Example of using websockets and a proxy
    • User-based file persistence with simple encoding/encryption
    • Sharing a client between multiple threads
  • Converted the unit tests to use Catch2
  • All library exceptions are now properly derived from the mqtt::exception base class.
  • [#231] Added on_disconnected callback to handle receipt of disconnect packet from server.
  • [#211, #223, #235] Removed use of Log() function from the Paho C library.
  • [#227] Fixed race condition in thread-safe queue
  • [#224] & [#255] Subscribing to MQTT v3 broker with array of one topic causes segfault.
  • [#282] Ability to build Debian/Ubuntu package
  • [#300] Calling reconnect() was hanging forever, even when successful. In addition several of the synchronous client calls were hanging forever on failure. They now properly throw a timeout_error exception.
  • Several memory issues and bug fixes from updated Paho C library support.

Catch2 Unit Tests

Unit tests were converted to use Catch2 for the test framework.

Catch2 can be found here: Catch2

Contributing

Contributions to this project are gladly welcomed and appreciated Before submitting a Pull Request, please keep three things in mind:

  • This is an official Eclipse project, so it is required that all contributors sign an Eclipse Contributor Agreement (ECA)
  • Please submit all Pull Requests against the develop branch (not master).
  • Please sign all commits.

For full details, see CONTRIBUTING.md.

Building from source

CMake is a cross-platform build system suitable for Unix and non-Unix platforms such as Microsoft Windows. It is now the only supported build system.

The Paho C++ library requires the Paho C library, v1.3.8 or greater, to be built and installed first. More information below.

CMake allows for options to direct the build. The following are specific to Paho C++:

Variable Default Value Description
PAHO_BUILD_SHARED TRUE (Linux), FALSE (Win32) Whether to build the shared library
PAHO_BUILD_STATIC FALSE (Linux), TRUE (Win32) Whether to build the static library
PAHO_BUILD_DOCUMENTATION FALSE Create and install the HTML based API documentation (requires Doxygen)
PAHO_BUILD_SAMPLES FALSE Build sample programs
PAHO_BUILD_TESTS FALSE Build the unit tests. (This requires Catch2)
PAHO_WITH_SSL TRUE (Linux), FALSE (Win32) Flag that defines whether to build ssl-enabled binaries too
PAHO_BUILD_DEB_PACKAGE FALSE Flag that configures cpack to build a Debian/Ubuntu package

In addition, the C++ build might commonly use CMAKE_PREFIX_PATH to help the build system find the location of the Paho C library.

Unix and Linux

On *nix systems CMake creates Makefiles.

The build process currently supports a number of Unix and Linux flavors. The build process requires the following tools:

  • CMake v3.5 or newer
  • GCC v4.8 or newer or Clang v3.9 or newer
  • GNU Make

On Debian based systems this would mean that the following packages have to be installed:

$ sudo apt-get install build-essential gcc make cmake cmake-gui cmake-curses-gui

If you will be using secure sockets (and you probably should):

$ sudo apt-get install libssl-dev 

Building the documentation requires doxygen and optionally graphviz to be installed:

$ sudo apt-get install doxygen graphviz

Unit tests are being built using Catch2.

Catch2 can be found here: Catch2. You must download and install Catch2 to build and run the unit tests locally.

Building the Paho C library

Before building the C++ library, first, build and install the Paho C library, if not already present. Note, this version of the C++ library requires Paho C v1.3.8 or greater.

$ git clone https://github.com/eclipse/paho.mqtt.c.git
$ cd paho.mqtt.c
$ git checkout v1.3.8

$ cmake -Bbuild -H. -DPAHO_ENABLE_TESTING=OFF -DPAHO_BUILD_STATIC=ON \
    -DPAHO_WITH_SSL=ON -DPAHO_HIGH_PERFORMANCE=ON
$ sudo cmake --build build/ --target install
$ sudo ldconfig

This builds with SSL/TLS enabled. If that is not desired, omit the -DPAHO_WITH_SSL=ON.

It also uses the "high performace" option of the C library to disable more extensive internal memory checks. Remove the PAHO_HIGH_PERFORMANCE option (i.e. turn it off) to debug memory issues, but for most production systems, leave it on for better performance.

To install the library to a non-standard location, use the CMAKE_INSTALL_PREFIX to specify a location. For example, to install into under the build directory, perhaps for local testing, do this:

$ cmake -Bbuild -H. -DPAHO_ENABLE_TESTING=OFF -DPAHO_BUILD_STATIC=ON \
    -DPAHO_WITH_SSL=ON -DPAHO_HIGH_PERFORMANCE=ON \
    -DCMAKE_INSTALL_PREFIX=./build/_install

Building the Paho C++ library

An example CMake build session might look like this:

$ git clone https://github.com/eclipse/paho.mqtt.cpp
$ cd paho.mqtt.cpp

$ cmake -Bbuild -H. -DPAHO_BUILD_STATIC=ON \
    -DPAHO_BUILD_DOCUMENTATION=TRUE -DPAHO_BUILD_SAMPLES=TRUE
$ sudo cmake --build build/ --target install
$ sudo ldconfig

If you did not install Paho C library to a default system location or you want to build against a different version, use the CMAKE_PREFIX_PATH to specify its install location. Perhaps something like this:

$ cmake -Bbuild -H. -DPAHO_BUILD_DOCUMENTATION=ON -DPAHO_BUILD_SAMPLES=ON \
    -DPAHO_BUILD_STATIC=ON \
    -DCMAKE_PREFIX_PATH=$HOME/mqtt/paho.mqtt.c/build/_install

To use another compiler, either the CXX environment variable can be specified in the configuration step:

$ CXX=clang++ cmake ..

or the CMAKE_CXX_COMPILER flag can be used:

$ cmake -DCMAKE_CXX_COMPILER=clang++

Building a Debian/Ubuntu package

$ cmake -Bbuild -H. -DPAHO_WITH_SSL=ON -DPAHO_ENABLE_TESTING=OFF -DPAHO_BUILD_DEB_PACKAGE=ON
$ cmake --build build
$ (cd build && cpack)

will generate a .deb file.

Windows

On Windows systems CMake creates Visual Studio project files.

The build process currently supports a number Windows versions. The build process requires the following tools:

  • CMake GUI v3.5 or newer
  • Visual Studio 2015 or newer

First install and open the cmake-gui application. This tutorial is based on cmake-gui 3.5.2.

Second, select the path to the Paho MQTT C library (CMAKE_PREFIX_PATH) if not installed in a standard path. Remember that the Paho MQTT C must be installed on the system. Next, choose if it is supposed to build the documentation (PAHO_BUILD_DOCUMENTATION) and/or the sample applications (PAHO_BUILD_SAMPLES).

Once the configuration is done, click on the Configure button, select the version of the Visual Studio, and then click on Generate button.

At the end of this process you have a Visual Studio solution.

Alternately, the libraries can be completely built at an MSBuild Command Prompt. Download the Paho C and C++ library sources, then open a command window and first compile the Paho C library:

> cd paho.mqtt.c
> cmake -Bbuild -H. -DCMAKE_INSTALL_PREFIX=C:\mqtt\paho-c
> cmake --build build/ --target install

Then build the C++ library:

> cd ..\paho.mqtt.cpp
> cmake -Bbuild -H. -DCMAKE_INSTALL_PREFIX=C:\mqtt\paho-cpp -DPAHO_BUILD_SAMPLES=ON -DPAHO_WITH_SSL=OFF -DCMAKE_PREFIX_PATH=C:\mqtt\paho-c
> cmake --build build/ --target install

This builds and installs both libraries to a non-standard location under C:\mqtt. Modify this location as desired or use the default location, but either way, the C++ library will most likely need to be told where the C library was built using CMAKE_PREFIX_PATH.

It seems quite odd, but even on a 64-bit system using a 64-bit compiler, MSVC seems to default to a 32-bit build target.

The 64-bit target can be selected using tge CMake generator switch, -G, at configuration time. The full version must be provided. For Visual Studio 2015 which is v14 do this to first build the Paho C library:

> cmake -G "Visual Studio 14 Win64" -Bbuild -H. -DCMAKE_INSTALL_PREFIX=C:\mqtt\paho-c
...

Then use it to build the C++ library:

> cmake -G "Visual Studio 14 Win64" -Bbuild -H. -DCMAKE_INSTALL_PREFIX=C:\mqtt\paho-cpp -DPAHO_WITH_SSL=OFF -DCMAKE_PREFIX_PATH=C:\mqtt\paho-c
...

Note that it is very important that you use the same generator (target) to build BOTH libraries, otherwise you will get lots of linker errors when you try to build the C++ library.

Supported Network Protocols

The library supports connecting to an MQTT server/broker using TCP, SSL/TLS, and websockets (secure and unsecure). This is chosen by the URI supplied to the connect() call. It can be specified as:

"tcp://<host>:<port>"  - TCP (unsecure)
"ssl://<host>:<port>"  - SSL/TLS
"ws://<host>:<port>"   - Unsecure websockets
"wss://<host>:<port>"  - Secure websockets

Note that to use "ssl://" or "wss://" you must compile the library with OpenSSL, and you must supply a set of ssl_options with the connect_options.

Example

Sample applications can be found in the source repository at src/samples: https://github.com/eclipse/paho.mqtt.cpp/tree/master/src/samples

This is a partial example of what a typical example might look like:

int main(int argc, char* argv[])
{
    sample_mem_persistence persist;
    mqtt::client cli(ADDRESS, CLIENT_ID, &persist);

    callback cb;
    cli.set_callback(cb);

    auto connOpts = mqtt::connect_options_builder() 
        .keep_alive_interval(20);
        .clean_session()
        .finalize();

    try {
        cli.connect(connOpts);

        // First use a message pointer.

        mqtt::message_ptr pubmsg = mqtt::make_message(PAYLOAD1);
        pubmsg->set_qos(QOS);
        cli.publish(TOPIC, pubmsg);

        // Now try with itemized publish.

        cli.publish(TOPIC, PAYLOAD2, strlen(PAYLOAD2)+1, 0, false);

        // Disconnect
        
        cli.disconnect();
    }
    catch (const mqtt::persistence_exception& exc) {
        cerr << "Persistence Error: " << exc.what() << " ["
            << exc.get_reason_code() << "]" << endl;
        return 1;
    }
    catch (const mqtt::exception& exc) {
        cerr << "Error: " << exc.what() << " ["
            << exc.get_reason_code() << "]" << endl;
        return 1;
    }

    return 0;
}

The original API organization and documentation were adapted from:

The Paho Java library by Dave Locke et al. Copyright (c) 2012, IBM Corp

All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html


This code requires:

The Paho C library by Ian Craggs Copyright (c) 2013-2018, IBM Corp.

All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 and Eclipse Distribution License v1.0 which accompany this distribution.

The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution License is available at http://www.eclipse.org/org/documents/edl-v10.php.

More Repositories

1

mosquitto

Eclipse Mosquitto - An open source MQTT broker
C
7,649
star
2

che

Kubernetes based Cloud Development Environments for Enterprise Teams
TypeScript
6,868
star
3

jetty.project

Eclipse Jetty® - Web Container & Clients - supports HTTP/2, HTTP/1.1, HTTP/1.0, websocket, servlets, and more
Java
3,655
star
4

paho.mqtt.android

MQTT Android
Java
2,708
star
5

paho.mqtt.golang

Go
2,381
star
6

eclipse-collections

Eclipse Collections is a collections framework for Java with optimized data structures and a rich, functional and fluent API.
Java
2,283
star
7

paho.mqtt.java

Eclipse Paho Java MQTT client library. Paho is an Eclipse IoT project.
Java
2,095
star
8

paho.mqtt.python

paho.mqtt.python
Python
1,946
star
9

sumo

Eclipse SUMO is an open source, highly portable, microscopic and continuous traffic simulation package designed to handle large networks. It allows for intermodal simulation including pedestrians and comes with a large set of tools for scenario creation.
1,902
star
10

paho.mqtt.c

An Eclipse Paho C client library for MQTT for Windows, Linux and MacOS. API documentation: https://eclipse.github.io/paho.mqtt.c/
C
1,736
star
11

eclipse.jdt.ls

Java language server
Java
1,410
star
12

mraa

Linux Library for low speed IO Communication in C with bindings for C++, Python, Node.js & Java. Supports generic io platforms, as well as Intel Edison, Intel Joule, Raspberry Pi and many more.
C
1,349
star
13

paho.mqtt.embedded-c

Paho MQTT C client library for embedded systems. Paho is an Eclipse IoT project (https://iot.eclipse.org/)
C
1,307
star
14

openvsx

An open-source registry for VS Code extensions
Java
1,181
star
15

paho.mqtt.javascript

paho.mqtt.javascript
JavaScript
1,145
star
16

milo

Eclipse Milo™ - an open source implementation of OPC UA (IEC 62541).
Java
976
star
17

omr

Eclipse OMR™ Cross platform components for building reliable, high performance language runtimes
C++
917
star
18

xtext

Eclipse Xtext™ is a language development framework
Java
715
star
19

upm

UPM is a high level repository that provides software drivers for a wide variety of commonly used sensors and actuators. These software drivers interact with the underlying hardware platform through calls to MRAA APIs.
C++
651
star
20

microprofile

Repository for important documentation - the index to the project / community
Java
635
star
21

californium

CoAP/DTLS Java Implementation
Java
620
star
22

leshan

Java Library for LWM2M
Java
614
star
23

paho.mqtt-spy

mqtt-spy is an open source desktop & command line utility intended to help you with monitoring activity on MQTT topics
Java
605
star
24

steady

Analyses your Java applications for open-source dependencies with known vulnerabilities, using both static analysis and testing to determine code context and usage for greater accuracy. https://eclipse.github.io/steady/
Java
518
star
25

sprotty

A diagramming framework for the web
TypeScript
514
star
26

paho.mqtt.m2mqtt

C#
513
star
27

jifa

🔬 Online Heap Dump, GC Log, Thread Dump & JFR File Analyzer.
Java
509
star
28

buildship

The Eclipse Plug-ins for Gradle project.
Java
507
star
29

lsp4j

A Java implementation of the language server protocol intended to be consumed by tools and language servers implemented in Java.
Java
473
star
30

kura

Eclipse Kura™ project
Java
469
star
31

wakaama

Eclipse Wakaama is a C implementation of the Open Mobile Alliance's LightWeight M2M protocol (LWM2M).
C
465
star
32

streamsheets

An open-source tool for processing stream data using a spreadsheet-like interface.
JavaScript
449
star
33

paho.mqtt.rust

paho.mqtt.rust
Rust
422
star
34

hawkbit

Eclipse hawkBit™
Java
416
star
35

eclipse-collections-kata

Eclipse Collections Katas
Java
411
star
36

hono

Eclipse Hono™ Project
Java
378
star
37

repairnator

Software development bots for Github. Join the bot revolution! 🌟🤖🌟💞
Java
370
star
38

ponte

Ponte Project
JavaScript
360
star
39

birt

Eclipse BIRT™ The open source reporting and data visualization project.
Java
355
star
40

paho.golang

Go libraries
Go
324
star
41

rdf4j

Eclipse RDF4J: scalable RDF for Java
Java
323
star
42

paho.mqtt-sn.embedded-c

Paho C MQTT-SN gateway and libraries for embedded systems. Paho is an Eclipse IoT project.
C++
313
star
43

lemminx

XML Language Server
Java
255
star
44

vorto

Vorto Project
Java
221
star
45

tahu

Eclipse Tahu addresses the existence of legacy SCADA/DCS/ICS protocols and infrastructures and provides a much-needed definition of how best to apply MQTT into these existing industrial operational environments.
Java
220
star
46

kapua

Java
218
star
47

elk

Eclipse Layout Kernel - Automatic layout for Java applications.
Java
211
star
48

jnosql

Eclipse JNoSQL is a framework which has the goal to help Java developers to create Jakarta EE applications with NoSQL.
Java
210
star
49

corrosion

Eclipse Corrosion - Rust edition in Eclipse IDE
Java
199
star
50

capella

Open Source Solution for Model-Based Systems Engineering
Java
197
star
51

dirigible

Eclipse Dirigible™ Project
JavaScript
196
star
52

microprofile-config

MicroProfile Configuration Feature
Java
182
star
53

wildwebdeveloper

Simple and productive Web Development Tools in the Eclipse IDE
Java
181
star
54

pdt

PHP Development Tools project (PDT)
PHP
178
star
55

org.aspectj

Java
172
star
56

xacc

XACC - eXtreme-scale Accelerator programming framework
C++
137
star
57

thingweb.node-wot

thingweb.node-wot
TypeScript
130
star
58

microprofile-rest-client

MicroProfile Rest Client
Java
124
star
59

tycho

Tycho project repository (tycho)
Java
119
star
60

microprofile-conference

Microprofile.io Demo Code - Web Services Conference Application
Java
117
star
61

microprofile-fault-tolerance

microprofile fault tolerance
Java
115
star
62

microprofile-samples

Micro Profile Samples
Java
115
star
63

xtext-core

xtext-core
Java
114
star
64

microprofile-open-api

Microprofile open api
Java
112
star
65

jbom

Java
111
star
66

gef

Eclipse GEF™
Java
111
star
67

transformer

Eclipse Transformer provides tools and runtime components that transform Java binaries, such as individual class files and complete JARs and WARs, mapping changes to Java packages, type names, and related resource names.
Java
108
star
68

paho.mqtt.testing

An Eclipse Paho project - a Python broker for testing
Python
104
star
69

tinydtls

Eclipse tinydtls
C
102
star
70

xtext-xtend

xtext-xtend
Java
100
star
71

microprofile-graphql

microprofile-graphql
Java
98
star
72

microprofile-lra

microprofile-lra
Java
97
star
73

microprofile-health

microprofile-health
Java
95
star
74

microprofile-metrics

microprofile-metrics
Java
94
star
75

kuksa.val

kuksa.val
C++
93
star
76

sw360

SW360 project
Java
92
star
77

microprofile-jwt-auth

Java
92
star
78

mosaic

Eclipse MOSAIC is a Multi-Domain and Multi-Scale Simulation Framework for Automated and Connected Mobility Scenarios.
Java
88
star
79

nebula

Nebula Project
Java
84
star
80

microprofile-reactive-streams-operators

Microprofile project
Java
79
star
81

mosquitto.rsmb

Mosquitto rsmb
C
75
star
82

microprofile-starter

MicroProfile project generator source code
Java
69
star
83

aCute

Eclipse aCute - C# edition in Eclipse IDE
Java
65
star
84

ditto-examples

Eclipse Ditto™: Digital Twin framework - Examples
Java
63
star
85

epsilon

Epsilon is a family of Java-based scripting languages for automating common model-based software engineering tasks, such as code generation, model-to-model transformation and model validation, that work out of the box with EMF (including Xtext and Sirius), UML (including Cameo/MagicDraw), Simulink, XML and other types of models.
Java
61
star
86

lsp4e

Language Server Protocol support in Eclipse IDE
Java
60
star
87

tm4e

TextMate support in Eclipse IDE
Java
60
star
88

californium.tools

Californium project
Java
59
star
89

microprofile-reactive-messaging

Java
59
star
90

microprofile-opentracing

microprofile-opentracing
Java
57
star
91

eclemma

🌘 Java Code Coverage for Eclipse IDE
Java
57
star
92

texlipse

Eclipse Texlipse
Java
56
star
93

mita

mita
Xtend
55
star
94

dartboard

Dart Plugin for Eclipse
Java
55
star
95

jnosql-databases

This project contains Eclipse JNoSQL databases
Java
54
star
96

windowbuilder

Eclipse Windowbuilder
Java
54
star
97

xtext-eclipse

xtext-eclipse
Java
49
star
98

adore

Eclipse ADORe is a ROS based modular software library and toolkit for decision making, planning, control and simulation of automated vehicles supporting CARLA and SUMO.
Makefile
48
star
99

packages

IoT Packages project
Smarty
46
star
100

amlen

Message Broker for IoT/Mobile/Web. Mainly uses MQTT v3.x and v5. Aims to be easy to use, scalable and reliable
C
46
star