Contents

arnecs/MQTTKit

MQTT Client written in Swift

Getting Started

Installing

Swift Package Manager

MQTTKit can be installed using Swift Package Manager, add MQTTKit to the dependency list in Package.swift:

let package = Package(
    /*...*/
    dependencies: [
        .package(url: "https://github.com/arnecs/MQTTKit.git", from: "0.1.0")
    ]
    /*...*/
)
Carthage

MQTTKit can be installed using Carthage, add this line in your Carthage file:

github "arnecs/MQTTKit"
Manually

Clone the project repository, and add it to the your project.

git clone https://github.com/arnecs/MQTTKit.git

Usage

Use closures to get notified of events


public var didRecieveMessage: ((_ mqtt: MQTTClient, _ message: MQTTMessage) -> Void)?
public var didRecieveConack: ((_ mqtt: MQTTClient, _ status: MQTTConnackResponse) -> Void)?
public var didSubscribe: ((_ mqtt: MQTTClient, _ topic: String) -> Void)?
public var didUnsubscribe: ((_ mqtt: MQTTClient, _ topic: String) -> Void)?
public var didConnect: ((_ mqtt: MQTTClient, _ connected: Bool) -> Void)?
public var didDisconnect: ((_ mqtt: MQTTClient, _ error: Error?) -> Void)?
public var didChangeState: ((_ mqtt: MQTTClient, _ state: MQTTConnectionState) -> Void)?

Connect to a MQTT Broker

import MQTTKit

let mqtt = MQTTClient(host: "localhost")

mqtt.didConnect = {mqtt, connected in
  // Gets called when a connack.accepted is recieved
}
// Or
mqtt.didRecieveConnack = {mqtt, response in
  // Gets callen when connack is recieved
}

mqtt.connect()

Subscribe and unsubscribe to topics

mqtt.didSubscribe = {mqtt, topic in
  // Gets called when suback is recieved
}

mqtt.didUnsubscribe  = {mqtt, topic in
  // Gets called when unsuback is recieved
}

mqtt.subscribe(to: "/my/topic")
mqtt.unsubscribe(from: "/my/topic")

Publish and recieved messages

let messagePayload = "Some interesting content".data(using: .utf8)!
mqtt.publish(to: "/my/topic", payload: messagePayload, qos: .QoS0, retained: false)

mqtt.didRecieveMessage = {mqtt, message in
  print(message)
}

Running the tests

The tests requires a connection to a MQTT Broker. The address is located in the test file.

To start the tests, run:

swift test

or use xcodebuild to test for a specific platform:

xcodebuild clean test -scheme MQTTKit -destination "platform=macos"
xcodebuild clean test -scheme MQTTKit -destination "platform=iOS Simulator,name=iPhone 8"
xcodebuild clean test -scheme MQTTKit -destination "platform=tvOS Simulator,name=Apple TV"

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Package Metadata

Repository: arnecs/MQTTKit

Stars: 9

Forks: 9

Open issues: 1

Default branch: master

Primary language: swift

License: MIT

Topics: carthage, ios, iot, macos, mqtt, mqtt-broker, swift, xcode

README: README.md