MQTT Client written in Swift


MQTT Client written in Swift

Travis Swift Version license

Getting Started


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: "", from: "0.1.0")


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

github "arnecs/MQTTKit"


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

git clone


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


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

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"


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


  • Swift Tools 4.0.0
View More Packages from this Author


  • None
Last updated: Sun Mar 24 2024 16:07:33 GMT-0900 (Hawaii-Aleutian Daylight Time)