MQTT Client in Swift
- Fully written in Swift
- Robust error handling
- Reconnection logic
- Performant
mqttSession = MQTTSession(host: "localhost", port: 1883, clientID: "swift", cleanSession: true, keepAlive: 15, useSSL: false)
mqttSession.connect { (succeeded, error) -> Void in
if succeeded {
print("Connected!")
}
}
mqttSession.subscribe(to: "/hey/cool", delivering: .atLeastOnce) { (succeeded, error) -> Void in
if succeeded {
print("Subscribed!")
}
}
mqttSession.unSubscribe(from: ["/ok/cool", "/no/ok"]) { (succeeded, error) -> Void in
if succeeded {
print("unSubscribed!")
}
}
let jsonDict = ["hey" : "sup"]
let data = try! JSONSerialization.data(withJSONObject: jsonDict, options: .prettyPrinted)
mqttSession.publish(data, in: "/hey/wassap", delivering: .atLeastOnce, retain: false) { (succeeded, error) -> Void in
if succeeded {
print("Published!")
}
}
mqttSession.delegate = self
func mqttDidReceive(message: MQTTMessage, from session: MQTTSession) {
let string = message.description
}
func mqttDidDisconnect(session: MQTTSession, reson: MQTTSessionDisconnect, error: Error?) {
}
Install using CocoaPods by adding the following lines to your Podfile:
use_frameworks!
pod 'SwiftMQTT', :git => 'https://github.com/Nautiyalsachin/SwiftMQTT'
MIT