Skip to content

Nautiyalsachin/SwiftMQTT

This branch is 39 commits ahead of, 6 commits behind aciidgh/SwiftMQTT:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

09b0774 · Oct 4, 2017

History

68 Commits
Aug 28, 2017
Aug 28, 2017
Aug 28, 2017
Oct 2, 2017
Sep 16, 2016
Jun 28, 2016
Oct 2, 2017
Aug 28, 2017
Sep 16, 2016

Repository files navigation

SwiftMQTT

MQTT Client in Swift

Build Status Version License

Info

  • Fully written in Swift
  • Robust error handling
  • Reconnection logic
  • Performant

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 mqttDidReceive(message: MQTTMessage, from session: MQTTSession) {
	let string = message.description
}
func mqttDidDisconnect(session: MQTTSession, reson: MQTTSessionDisconnect, error: Error?) {
}

Installation

CocoaPods

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

use_frameworks!
pod 'SwiftMQTT', :git => 'https://github.com/Nautiyalsachin/SwiftMQTT'

License

MIT

About

MQTT Client in pure swift ❤️

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 97.8%
  • Ruby 1.3%
  • Objective-C 0.9%