Skip to content

muzzley/SwiftMQTT

 
 

Repository files navigation

SwiftMQTT

MQTT Client in pure Swift ❤️️

Build Status Version License

Info

Sample Project Screenshot

How to use

Create Session

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

Connect

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

Subscribe

mqttSession.subscribe(to: "/hey/cool", delivering: .atLeastOnce) { (succeeded, error) -> Void in
 if succeeded {
    print("Subscribed!")
  }
}

Unsubscribe

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

Publish

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

Conform to MQTTSessionDelegate to receive messages

mqttSession.delegate = self
func mqttSession(session: MQTTSession, received message: Data, in topic: String) {
	let string = String(data: message, encoding: .utf8)!
}

Installation

CocoaPods

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

use_frameworks!
pod 'SwiftMQTT'  

License

MIT

About

MQTT Client in pure swift ❤️

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Swift 96.7%
  • Ruby 2.0%
  • Objective-C 1.3%