Latest 3.0.1
Homepage https://github.com/aciidb0mb3r/SwiftMQTT
License MIT
Platforms ios 8.0, osx 10.10, tvos 9.0
Frameworks Foundation
Authors

MQTT Client

Build Status
Version
License

Usage

Create Session

mqttSession = MQTTSession(
    host: "localhost",
    port: 1883,
    clientID: "swift", // must be unique to the client
    cleanSession: true,
    keepAlive: 15,
    useSSL: false
)

Connect

mqttSession.connect { error in
    if error == .none {
        print("Connected!")
    } else {
        print(error.description)
    }
}

Subscribe

let topic = "mytopic" 
mqttSession.subscribe(to: topic, delivering: .atLeastOnce) { error in
    if error == .none {
        print("Subscribed to (topic)!")
    } else {
        print(error.description)
    }
}

Unsubscribe

let topic = "mytopic"
mqttSession.unSubscribe(from: topic) { error in
    if error == .none {
        print("Unsubscribed from (topic)!")
    } else {
        print(error.description)
    }
}

Publish

let json = ["key" : "value"]
let data = try! JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
let topic = "mytopic"

mqttSession.publish(data, in: topic, delivering: .atLeastOnce, retain: false) { error in
    if error == .none {
        print("Published data in (topic)!")
    } else {
        print(error.description)
    }
}

Conform to MQTTSessionDelegate to receive messages

mqttSession.delegate = self
func mqttDidReceive(message: MQTTMessage, from session: MQTTSession) {
    print(message.topic)
    print(message.stringRepresentation)
}
func mqttDidDisconnect(session: MQTTSession, error: MQTTSessionError) {
    if error == .none {
        print("Successfully disconnected from MQTT broker")
    } else {
        print(error.description)
    }
}

Installation

CocoaPods

Install using CocoaPods by adding the following lines to your Podfile:

target 'MyApp' do
    use_frameworks!
    pod 'SwiftMQTT'
end

Carthage

github "aciidb0mb3r/SwiftMQTT"

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/aciidb0mb3r/SwiftMQTT.git", from: "3.0.0")
]

License

MIT

Latest podspec

{
    "name": "SwiftMQTT",
    "version": "3.0.1",
    "summary": "MQTT Client in pure Swift",
    "description": "MQTT Client in Swift 4.1 based on MQTT Version 3.1.1",
    "homepage": "https://github.com/aciidb0mb3r/SwiftMQTT",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Ankit Agarwal": "[email protected].com"
    },
    "source": {
        "git": "https://github.com/aciidb0mb3r/SwiftMQTT.git",
        "tag": "3.0.1"
    },
    "swift_version": "4.1",
    "platforms": {
        "ios": "8.0",
        "osx": "10.10",
        "tvos": "9.0"
    },
    "source_files": "SwiftMQTT/SwiftMQTT/**/*.swift",
    "frameworks": "Foundation"
}

Pin It on Pinterest

Share This