Skip to content

ngwing/SwiftMQTT

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftMQTT

MQTT Client in pure swift ❤️

Master: Build Status

Info

Sample Project Screenshot

How to use

Create MQTTSession object:

mqttSession = MQTTSession(host: "localhost", port: 1883, clientID: "swift", cleanSession: true, keepAlive: 15)

Connect

mqttSession.connect { (succeeded, error) -> Void in
  if succeeded {
    print("Connected!")
  }
}

Subscribe

mqttSession.subscribe("/hey/cool", qos: MQTTQoS.AtLeastOnce) { (succeeded, error) -> Void in
 if succeeded {
    print("Subscribed!")
  }
}

Unsubscribe

 mqttSession.unSubscribe(["/ok/cool", "/no/ok"]) { (succeeded, error) -> Void in
  if succeeded {
    print("unSubscribed!")
  }
}

Publish

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!")
  }
}

Conform to MQTTSessionDelegate to receive messages

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)")
}

License

MIT

About

MQTT Client in pure swift ❤️

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Swift 98.7%
  • Objective-C 1.3%