HiveMQ Logo
MQTT-Client-Framework - MQTT Client Library Encyclopedia

MQTT-Client-Framework - MQTT Client Library Encyclopedia

author Christoph Krey

Written by Christoph Krey

Category: MQTT MQTT Client MQTT Client Library

Published: July 20, 2015

Short info

MQTT-Client-Framework
Language Objective-C
License Eclipse Public License - v 1.0
Website github.com/ckrey/MQTT-Client-Framework
API Style Asynchronous and Blocking

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

Feature
MQTT 3.1 ok
MQTT 3.1.1 ok
LWT ok
SSL/TLS ok
Automatic Reconnect ok
Disk Persistence ok
Feature
QoS 0 ok
QoS 1 ok
QoS 2 ok
Authentication ok
Throttling ok
Offline Message Buffering ok

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
    1
    
    /MQTTClient/dist/MQTTClient.framework
    to your iOS project.



  • Include Source
    After cloning the repo from github you may add the sources under
    1
    
    /MQTTClient/MQTTClient
    directly to your iOS project.

Doxygen generated documentation is part of the repo under

1
/MQTTClient/dist/documentation/html

Connect

1
2
3
4
5
#import "MQTTClient.h"

MQTTSession *session = [[MQTTSession alloc] init];

[session connectToHost:@"192.168.0.1" port:1883 usingSSL:NO];

Connect with MQTT 3.1 or MQTT 3.1.1

1
2
3
session.protocolLevel = 4;

[session connectToHost:@"192.168.0.1" port:1883 usingSSL:NO];

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

1
2
3
4
5
6
session.willFlag = TRUE;
session.willTopic = @"example/status";
session.willMsg = [@"offline" dataUsingEncoding:NSUTF8StringEncoding];
session.willQoS = MQTTQosLevelExactlyOnce;

[session connectToHost:@"192.168.0.1" port:1883 usingSSL:NO];

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

1
2
3
4
session.userName = @"myname";
session.password = @"secret";

[session connectToHost:@"192.168.0.1" port:1883 usingSSL:NO];

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

1
2
3
4
[session publishData:[@"Sample Data" dataUsingEncoding:NSUTF8StringEncoding]
    topic:@"example/data"
    retain:NO
    qos:MQTTQosLevelAtMostOnce];

Publish a retained Message

1
2
3
4
[session publishData:[@"Sample Data" dataUsingEncoding:NSUTF8StringEncoding]
    topic:@"example/data"
    retain:YES
    qos:MQTTQosLevelExactlyOnce];

Subscribe

1
[session subscribeToTopic:@"example/#" atLevel:MQTTQosLevelExactlyOnce];

Unsubscribe

1
[session unsubscribeTopic:@"example/#"];

Disconnect

1
[session close];

Using TLS / SSL

You may use iOS’ keyChain to validate server certificates:

1
[session connectToHost:@"192.168.0.1" port:8883 usingSSL:YES];

Or you may use a custom security policy:

1
2
3
4
5
6
7
NSString* certificate = [[NSBundle bundleForClass:[MQTTSession class]] pathForResource:@"certificate" ofType:@"cer"]; 
session.securityPolicy = [MQTTSSLSecurityPolicy policyWithPinningMode:MQTTSSLPinningModeCertificate];
session.securityPolicy.pinnedCertificates = @[ [NSData dataWithContentsOfFile:certificate] ];

session.securityPolicy.allowInvalidCertificates = YES;

[session connectToHost:@"192.168.0.1" port:8883 usingSSL:YES];

And you may use client certificates:

1
2
3
4
5
NSString *path = [[NSBundle bundleForClass:[MQTTClientTests class]] pathForResource:parameters[@„client"] ofType:@"p12"];

session.certificates = [MQTTSession clientCertsFromP12:path passphrase:@„secret];

[session connectToHost:@"192.168.0.1" port:8883 usingSSL:YES];

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 Christoph Krey

About Christoph Krey

Thanks for this guest blog post

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.

Follow Christoph on Github

mail icon Contact Christoph mail icon Contact HiveMQ
newer posts Paho Embedded - MQTT Client Library Encyclopedia
Eclipse Paho Java - MQTT Client Library Encyclopedia older posts