MQTT Client Library Encyclopedia – MQTT-Client-Framework
Guest post by Christoph Krey
|API-Style||Asynchronous and Blocking|
|License||Eclipse Public License – v 1.0|
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.
|Offline Message Buffering|
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
#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
session.protocolLevel = 4; [session connectToHost:@"192.168.0.1" port:1883 usingSSL:NO];
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
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];
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.
If the Will Flag is set to YES, the Will Topic is a string, nil otherwise.
If the Will Flag is set to YES the Will Message must be specified, nil otherwise.
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
session.userName = @"myname"; session.password = @"secret"; [session connectToHost:@"192.168.0.1" port:1883 usingSSL:NO];
an NSString object containing the user’s name (or ID) for authentication. May be nil.
an NSString object containing the user’s password. If userName is nil, password must be nil as well.
[session publishData:[@"Sample Data" dataUsingEncoding:NSUTF8StringEncoding] topic:@"example/data" retain:NO qos:MQTTQosLevelAtMostOnce];
Publish a retained Message
[session publishData:[@"Sample Data" dataUsingEncoding:NSUTF8StringEncoding] topic:@"example/data" retain:YES qos:MQTTQosLevelExactlyOnce];
[session subscribeToTopic:@"example/#" atLevel:MQTTQosLevelExactlyOnce];
Using TLS / SSL
You may use iOS’ keyChain to validate server certificates:
[session connectToHost:@"192.168.0.1" port:8883 usingSSL:YES];
Or you may use a custom security policy:
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:
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];
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
MQTTChat/iPhone Simulator-3. Messages are not retained. Each session subscribes to the topic
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