CocoaMQTT
MQTT v3.1.1 client library for iOS/macOS/tvOS written with Swift 5
Build
Build with Xcode 11.1 / Swift 5.1
Installation
CocoaPods
Install using CocoaPods by adding this line to your Podfile:
use_frameworks! # Add this if you are targeting iOS 8+ or using Swift
pod 'CocoaMQTT'
Then, run the following command:
$ pod install
Carthage
Install using Carthage by adding the following lines to your Cartfile:
github "emqx/CocoaMQTT" "master"
Then, run the following command:
$ carthage update --platform iOS,macOS,tvOS
Last if you're building for OS X:
- On your application targets “General” settings tab, in the "Embedded Binaries" section, drag and drop CocoaMQTT.framework from the Carthage/Build/Mac folder on disk.
If you're building for iOS, tvOS:
-
On your application targets “General” settings tab, in the "Linked Frameworks and Libraries" section, drag and drop each framework you want to use from the Carthage/Build folder on disk.
-
On your application targets "Build Phases" settings tab, click the "+" icon and choose "New Run Script Phase". Create a Run Script with the following contents:
/usr/local/bin/carthage copy-frameworks
-
and add the paths to the frameworks you want to use under "Input Files", e.g.:
$(SRCROOT)/Carthage/Build/iOS/CocoaMQTT.framework
Usage
Create a client to connect MQTT broker:
let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
let mqtt = CocoaMQTT(clientID: clientID, host: "localhost", port: 1883)
mqtt.username = "test"
mqtt.password = "public"
mqtt.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
mqtt.keepAlive = 60
mqtt.delegate = self
mqtt.connect()
Now you can use closures instead of CocoaMQTTDelegate
:
mqtt.didReceiveMessage = { mqtt, message, id in
print("Message received in topic \(message.topic) with payload \(message.string!)")
}
SSL Secure
One-way certification
No certificate is required locally. If you want to trust all untrust CA certificates, you can do this:
mqtt.allowUntrustCACertificate = true
Two-way certification
Need a .p12 file which is generated by a public key file and a private key file. You can generate the p12 file in the terminal:
openssl pkcs12 -export -clcerts -in client-cert.pem -inkey client-key.pem -out client.p12
MQTT over Websocket
In the 1.3.0, The CocoaMQTT has supported to connect to MQTT Broker by Websocket.
If you integrated by CocoaPods, you need to modify you Podfile
like the followings and execute pod install
again:
use_frameworks!
target 'Example' do
pod 'CocoaMQTT/WebSockets', '1.3.0-rc.2'
end
Then, Create a MQTT instance over Websocket:
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
let mqtt = CocoaMQTT(clientID: clientID, host: host, port: 8083, socket: websocket)
// ...
_ = mqtt.connect()
If you want to add additional custom header to the connection, you can use the following:
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
websocket.headers = [
"x-api-key": "value"
]
websocket.enableSSL = true
let mqtt = CocoaMQTT(clientID: clientID, host: host, port: 8083, socket: websocket)
// ...
_ = mqtt.connect()
Example App
You can follow the Example App to learn how to use it. But we need to make the Example App works fisrt:
$ cd Examples
$ pod install
Then, open the Example.xcworkspace/
by Xcode and start it!
Dependencies
These third-party functions are used:
- GCDAsyncSocket
- [Starscream](https://github.com/daltoniam/Starscream)
LICENSE
MIT License (see LICENSE
)
Contributors
Author
- Feng Lee feng@emqx.io
- CrazyWisdom zh.whong@gmail.com
- Alex Yu alexyu.dc@gmail.com
Github
link |
Stars: 1099 |
You may find interesting
Dependencies
Releases
- 2019-08-06T12:42:56
Features:
- Exponential Auto-Reconnect (#287 #290)
Bug fixes:
- Remove assert lines (#259)
- 2019-06-19T15:41:02
- Add closure parameter to didReceiveTrust (#280)
- Fix message re-deliver loop forever (#278 )
- 2019-06-14T13:45:00
Bug fixes:
- Make publishing process safe
- 2019-05-19T08:23:51
Bug fixes:
- Fix the SUBSCRIBE packet assembly error (#261)
1.2.0 - 2019-05-02T17:17:28
The CocoaMQTT v1.2.0 is available now!
Features:
- Support swift5 & Xcode 10.2
- Support to subscribe multiple topics at once
- Support to re-delivery the un-acknowledgment messages
Enhancements:
- Introduced the CocoaMQTTTimer (#246), which means the Timer doesn't block the
dispatchQueue
now - Change the Frame datatype to be a struct
- Fix the memory leak in the message publish functions
- Fix setting the dup/retained/qos flag error
- More testcases
- 2018-10-13T17:27:21
Update to Swift4.2
- Remove SwiftyTimer dependency (#228)
- Clean silos when re-established the connection (#221)
- Fix known bugs: Force unwrap, retained cycle
Shipping 1.1.0 - 2018-01-14T09:57:21
🎉 Swift 4 is comming!
- Support swift4 & Xcode 9.x (Thanks @manucheri 's PR https://github.com/emqtt/CocoaMQTT/pull/173)
- Add connection state callback function (#160)
- Clean & Ignore unnecessary/temp files
- Improve log format of Example project
- 2017-12-28T02:38:40
For Swift 3 & Xcode8 version
- Fixed memory leak at CocoaMQTTReader (#169, #168)
- Improve a little Example project
1.0.19 - 2017-07-25T08:07:06
- feat(buffer): let buffer timeout/silosMaxNumber can be configured (#154)
- refactor: delete CocoaMQTTDelegate func mqtt(_ mqtt: CocoaMQTT, didConnect host: String, port: Int)
1.0.18 - 2017-06-30T10:56:47
- dispatch timers on main queue (#151)
- (fix): make CocoaMQTTFrameBuffer thread safe (#141)
1.0.17 - 2017-06-01T06:18:06
- add usage of secure connection in README.md(#143)
- can allow trust a untrust CA certificate
1.0.16 - 2017-04-27T03:26:32
- add simple unit tests
- let Example Podfile directly reference local repo
- change the way maintain CocoaMQTT.socket (see #140)
1.0.15 - 2017-04-21T06:52:46
- Support tvOS(#129)
- Update README.md
- Add feature: auto reconnect.(#121)
- Add feature: flow control for publishing messages(#139)
1.0.14 - 2017-04-14T01:55:50
- Fix a unpack bug.(#138)
1.0.13 - 2017-04-13T06:03:53
- Let log have levels. (#135)
1.0.12 - 2017-04-11T07:51:03
- Adding publish complete delegate func
- Let objective-c see enum
- Make mqtt.subscriptions work.(#119 #85)
- Make possible using custom DispatchQueue
- Fix a crash. Can tolerate rescived publish message formatting errors.(#125)
1.0.11 - 2016-11-22T03:09:06
-
Fix crash if server returns reserved CONNACK message(https://github.com/emqtt/CocoaMQTT/issues/106)
-
Client SSL certificate support(https://github.com/emqtt/CocoaMQTT/issues/111)
-
CocoaMQTTClient: secureMQTT -> enableSSL
-
CocoaMQTTDelegate:
Add
optional func mqtt(_ mqtt: CocoaMQTT, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void)
-
Example code add client SSL certificate support
1.0.10 - 2016-10-10T01:17:24
Convert to Swift 3.0.
1.0.9 - 2016-09-07T06:00:10
- Remove published qos2 messages from cache if ReceivePubComp(#89).
- Change retained in CocoaMQTTMessage.swift to public (#90).
1.0.8 - 2016-09-02T07:52:13
- Fix frameReady() Causes Crash (#74).
- Fix msg_id > UInt16.max Causes Crash(#79).
- Pod dependency CocoaAsyncSocket updated to the version 7.5.0: Better IPv6 support(#86).
1.0.7 - 2016-05-26T13:29:16
- Fix delegate timing issue when using SSL(#71)
- Carthage readme fix
1.0.6 - 2016-04-25T09:23:30
1.add Podfile.lock 2.fix carthage update --platform iOS error:The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.
CocoaPod dependency - 2016-04-13T06:23:01
CocoaPod dependency setting
1.0.4 - 2016-03-12T04:30:51
Carthage support
1.0.3 - 2016-03-11T14:25:00
1.0.1 - 2016-03-11T12:04:34
1.retain -> retained 2.didPublishAck 3.support backgroundOnSocket 4.some fix 5.del enableBackgroundingOnSocket
CocoaMQTT 1.0.0 - 2015-12-16T02:52:02
universal swift framework, support CocoaPods.
CocoaMQTT 1.0-beta - 2015-09-21T07:22:54
The first public release
Support MQTT v3.1.1 protocol
Build with XCode 7 and Swift 2