MQTT Client Library Encyclopedia – MQTT-Client-Framework

Guest post by Christoph Krey

MQTT-Client-Framework
LanguageObjective-C
Website https://github.com/ckrey/MQTT-Client-Framework
API-Style Asynchronous and Blocking
LicenseEclipse Public License – v 1.0

Description

MQTT-Client-Framework is a native Objective-C iOS library. It uses CFNetwork for networking and CoreData for persistence. It is a complete implementation of MQTT 3.1.1 and supports TLS. Three flavors of API are available:A simple blocking API, an elaborate low level callback based API as well as a session manager which takes care of the iOS specific app life cycle.

When I started in 2013 developing the library based on the work of 2lemetry, there was no iOS MQTT client library existing which met the criteria for the OwnTracks project, especially TLS support and protocol completeness. Today there are some Objective-C and Swift wrappers for libmosquitto and Swift native implementations available as open source.
MQTT-Client-Framework is actively maintained and used in various projects. Contributions are welcome. It integrates well with Swift. The extensive test cases cover a huge percentage of MQTT 3.1.1 test cases for MQTT brokers.

Features

MQTT 3.1
MQTT 3.1.1ok
LWTok
SSL/TLSok
Automatic Reconnectok
Disk Persistenceok
QoS 0ok
QoS 1ok
QoS 2ok
Authenticationok
Throttlingok
Offline Message Bufferingok

Usage

Installation

There are 3 options to use MQTT-Client-Framework:

  • Use CocoaPods

    The simplest way is to add „MQTTClient“ to your Podfile and run „pod update“

  • Include Framework
    After cloning the repo from github you may add the iOS framework included under …/MQTTClient/dist/MQTTClient.framework to your iOS project.

  • Include Source

    After cloning the repo from github you may add the sources under …/MQTTClient/MQTTClient directly to your iOS project.

Doxygen generated documentation is part of the repo under /MQTTClient/dist/documentation/html

Connect

Connect with MQTT 3.1 or MQTT 3.1.1

protocolLevel
specifies the protocol to be used. The value of the Protocol Level field for the version 3.1.1 of the protocol is 4. The value for the version 3.1 is 3.

Connect with LWT

willFlag
If the Will Flag is set to YES this indicates that a Will Message MUST be published by the Server when the Server detects that the Client is disconnected for any reason other than the Client flowing a DISCONNECT Packet.

willTopic
If the Will Flag is set to YES, the Will Topic is a string, nil otherwise.

willMsg
If the Will Flag is set to YES the Will Message must be specified, nil otherwise.

willQoS
specifies the QoS level to be used when publishing the Will Message. If the Will Flag is set to NO, then the Will QoS MUST be set to 0. If the Will Flag is set to YES, the value of Will QoS can be 0 (0x00), 1 (0x01), or 2 (0x02).

Connect with Username / Password

userName
an NSString object containing the user’s name (or ID) for authentication. May be nil.

password
an NSString object containing the user’s password. If userName is nil, password must be nil as well.

Publish

Publish a retained Message

Subscribe

Unsubscribe

Disconnect

Using TLS / SSL

You may use iOS’ keyChain to validate server certificates:

Or you may use a custom security policy:

And you may use client certificates:

Example Application

You find a full iPhone application MQTTChat in github at https://github.com/ckrey/MQTTChat

MQTTChat implements a chat protocol over MQTT on an MQTT broker of your choice.

The application presents a tab view with 3 tabs to chat within the app or you may use the app on different devices. A separate MQTT session is used in each tab.

The app CONNECTs to the MQTT broker and sends a join message when the tab is first opened. The MQTT connection can be gracefully DISCONNECTed (after sending a leave message) and reCONNECTed on user request.

Simple text messages can be entered and send and will be displayed on all other connected clients and tabs.

The app publishes the text messages as UTF8 encoded strings to the MQTT broker to the topic <base>/<devicename>-<tabnumber>, e.g. MQTTChat/iPhone Simulator-3. Messages are not retained. Each session subscribes to the topic <base>/#, e.g. MQTTChat/#. Both PUBLISH and SUBSCRIBE use QoS 2.

To run the app, you need Xcode 6 and CocoaPods. You may change the connect parameters for your MQTT broker by updating the mqtt.plist file.

Author Information

Christoph Krey | OwnTracks UG (haftungsbeschränkt)
Christoph Krey is a freelance network application specialist who designs and implements client/server solutions in corporate environments since 1983. After a career in IT Management, he began his focus on protocols, mobile devices, and distributed applications. Christoph Krey maintains the MQTT client framework and has created MQTTInspector and OwnTracks for iOS.
   Website

4 comments

  1. sundeep Saluja says:

    HI,

    Where can i have callbacks when A Successful Publish/Subscribe occurs?

  2. Amirhossein Nateghi says:

    Hi,

    I implement this functionality but the automatic reconnecting doesn’t work, how can i implement this feature ?

  3. George says:

    How can we get to know that connection and subscription is established successfully.

  4. Andrey says:

    Emitted when the client receives any packet. This includes packets from subscribed topics as well as packets used by MQTT for managing subscriptions and connections

Leave a Reply

Your email address will not be published. Required fields are marked *