MQTT Client in pure swift ❤️
- Fully written in swift from ground up
- Closures everywhere :D
- 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)
mqttSession.connect { (succeeded, error) -> Void in
if succeeded {
print("Connected!")
}
}
mqttSession.subscribe("/hey/cool", qos: MQTTQoS.AtLeastOnce) { (succeeded, error) -> Void in
if succeeded {
print("Subscribed!")
}
}
mqttSession.unSubscribe(["/ok/cool", "/no/ok"]) { (succeeded, error) -> Void in
if succeeded {
print("unSubscribed!")
}
}
let jsonDict = ["hey" : "sup"]
let data = try! NSJSONSerialization.dataWithJSONObject(jsonDict, options: NSJSONWritingOptions.PrettyPrinted)
mqttSession.publishData(data, onTopic: "/hey/wassap", withQoS: MQTTQoS.AtLeastOnce, shouldRetain: false) { (succeeded, error) -> Void in
if succeeded {
print("Published!")
}
}
mqttSession.delegate = self
func mqttSession(session: MQTTSession, didReceiveMessage message: NSData, onTopic topic: String) {
let stringData = NSString(data: message, encoding: NSUTF8StringEncoding) as! String
print("data received on topic \(topic) message \(stringData)")
}
MIT