Latest 1.1.3
License MIT
Platforms osx 10.9, ios 8.0, tvos 9.0, requires ARC
Dependencies CocoaAsyncSocket
Authors , ,

Swift version

MQTT v3.1.1 client library for iOS/macOS/tvOS written with Swift 4


Build with Xcode 10 / Swift 4.2



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


Install using Carthage by adding the following lines to your Cartfile:

github "robbiehanson/CocoaAsyncSocket" "master"
github "emqtt/CocoaMQTT" "master"

Then, run the following command:

$ carthage update --platform iOS

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.:


Example in Example project:

let clientID = "CocoaMQTT-" + String(NSProcessInfo().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

Now you can use clousures instead of CocoaMQTTDelegate:

mqtt.didReceiveMessage = { mqtt, message, id in
    print("Message received in topic (message.topic) with payload (message.string!)")           

SSL Secure

  1. One-way certification

No certificate is required locally.
If you want to trust all untrust CA certificates, you can do this:

mqtt.allowUntrustCACertificate = true
  1. 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
 * Blueprint of the MQTT client
protocol CocoaMQTTClient {
    var host: String { get set }
    var port: UInt16 { get set }
    var clientID: String { get }
    var username: String? {get set}
    var password: String? {get set}
    var cleanSession: Bool {get set}
    var keepAlive: UInt16 {get set}
    var willMessage: CocoaMQTTWill? {get set}

    func connect() -> Bool
    func publish(_ topic: String, withString string: String, qos: CocoaMQTTQOS, retained: Bool, dup: Bool) -> UInt16
    func publish(_ message: CocoaMQTTMessage) -> UInt16
    func subscribe(_ topic: String, qos: CocoaMQTTQOS) -> UInt16
    func unsubscribe(_ topic: String) -> UInt16
    func ping()
    func disconnect()


 * MQTT Delegate
@objc public protocol CocoaMQTTDelegate {
    /// MQTT connected with server
    // deprecated: use mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) to tell if connect to the server successfully
    // func mqtt(_ mqtt: CocoaMQTT, didConnect host: String, port: Int)
    func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck)
    func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16)
    func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16)
    func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16 )
    func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopic topic: String)
    func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopic topic: String)
    func mqttDidPing(_ mqtt: CocoaMQTT)
    func mqttDidReceivePong(_ mqtt: CocoaMQTT)
    func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?)
    @objc optional func mqtt(_ mqtt: CocoaMQTT, didReceive trust: SecTrust, completionHandler: @escaping (Bool) -> Void)
    @objc optional func mqtt(_ mqtt: CocoaMQTT, didPublishComplete id: UInt16)
    @objc optional func mqtt(_ mqtt: CocoaMQTT, didStateChangeTo state: CocoaMQTTConnState)

AsyncSocket and Timer

These third-party functions are used:


MIT License (see LICENSE)




Latest podspec

    "name": "CocoaMQTT",
    "version": "1.1.3",
    "summary": "MQTT v3.1.1 client library for iOS and OS X written with Swift 4",
    "homepage": "",
    "license": {
        "type": "MIT"
    "authors": {
        "Feng Lee": "[email protected]",
        "CrazyWisdom": "[email protected]",
        "Alex Yu": "[email protected]"
    "requires_arc": true,
    "platforms": {
        "osx": "10.9",
        "ios": "8.0",
        "tvos": "9.0"
    "source": {
        "git": "",
        "tag": "1.1.3"
    "source_files": [
    "dependencies": {
        "CocoaAsyncSocket": [
            "~> 7.6.3"

Pin It on Pinterest

Share This