Skip to content

anatoliykant/SwiftMQTT

 
 

Repository files navigation

Platform Swift Carthage compatible Cocoapods compatible Swift Package Manager compatible

Donale

Donale Donale

License

SwiftMQTT

SwiftMQTT is an simple MQTT client written on Swift based on MQTT version 3.1.1

Features

Usage

Create Session

mqttSession = MQTTSession(
	host: "localhost",
	port: 1883,
	clientID: "swift", // must be unique to the client
	cleanSession: true,
	keepAlive: 15,
	useSSL: false
)

Connect

mqttSession.connect { error in
    if error == .none {
        print("Connected!")
    } else {
        print(error.description)
    }
}

Subscribe

let topic = "mytopic" 
mqttSession.subscribe(to: topic, delivering: .atLeastOnce) { error in
    if error == .none {
        print("Subscribed to \(topic)!")
    } else {
        print(error.description)
    }
}

Unsubscribe

let topic = "mytopic"
mqttSession.unSubscribe(from: topic) { error in
    if error == .none {
        print("Unsubscribed from \(topic)!")
    } else {
        print(error.description)
    }
}

Publish

let json = ["key" : "value"]
let data = try! JSONSerialization.data(withJSONObject: json, options: .prettyPrinted)
let topic = "mytopic"

mqttSession.publish(data, in: topic, delivering: .atLeastOnce, retain: false) { error in
    if error == .none {
        print("Published data in \(topic)!")
    } else {
        print(error.description)
    }
}

Conform to MQTTSessionDelegate to receive messages

mqttSession.delegate = self
func mqttDidReceive(message: MQTTMessage, from session: MQTTSession) {
    print(message.topic)
    print(message.stringRepresentation)
}
func mqttDidDisconnect(session: MQTTSession, error: MQTTSessionError) {
    if error == .none {
        print("Successfully disconnected from MQTT broker")
    } else {
        print(error.description)
    }
}

Installation

Carthage (recommended)

  1. Install Carthage 0.33.0 or later
  2. Add github "anatoliykant/SwiftMQTT" to your Cartfile
  3. Run carthage update --platform ios
  4. Drag SwiftMQTT.framework from the appropriate platform directory in Carthage/Build/ to the “Linked Frameworks and Libraries” section of your Xcode project’s “General” settings
  • On your application target’s “Build Phases” settings tab, click the “+” icon and choose “New Run Script Phase”. Create a Run Script with the following contents:
/usr/local/bin/carthage copy-frameworks

and add the paths to the frameworks you want to use under “Input Files”, e.g.:

$(SRCROOT)/Carthage/Build/iOS/SwiftMQTT.framework

This script works around an App Store submission bug triggered by universal binaries.

To integrate SwiftMQTT into your Xcode project using CocoaPods, specify it in your Podfile:

target 'MyApp' do
    use_frameworks!
    pod 'SwiftMQTT'
end

Swift Package Manager

Once you have your Swift package set up, adding SwiftMQTT as a dependency is as easy as adding it to the dependencies value of your Package.swift:

dependencies: [
    .package(url: "https://github.com/anatoliykant/SwiftMQTT.git", from: "3.0.0")
]

License

SwiftMQTT is released under the MIT license. See LICENSE for details

About

Elegant MQTT client in Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 97.4%
  • Ruby 1.6%
  • Objective-C 1.0%