MQTT Client in pure Swift ❤️️
- Fully written in Swift from ground up
- Closures everywhere 😃
- Includes test cases and sample project
- Based on MQTT Version 3.1.1 (http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc398718043)
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 mqttSession(session: MQTTSession, received message: Data, in topic: String) {
let string = String(data: message, encoding: .utf8)!
}
Install using CocoaPods by adding the following lines to your Podfile:
use_frameworks!
pod 'SwiftMQTT'
MIT