MQTT Client in pure swift ❤️
Switch branches/tags
Clone or download
adolfo Swift 4.1 Support (#32)
* Optimize a few things.
Create a public message structure to simplify and extend delegate.
Add background queue connect convenience method.
Have one delegate callback on disconnect.
Push errors out to delegate.
Rename SessionStream file.

* Fix compiler errors in test and sample app. Add comments for OCI changes.

* Make delegates weak references to avoid circular ownership
Add BatchReceiver sketch

* move MQTTMessage into models
make optional completion blocks truly optional

* Create MQTTBroker protocol

* create simple MQTTBatchingSession (pass-thru right now)

* MQTTConnectParams work in a framework

* fix bug when sending data in a disconnected state

* simplify life-cycle on connection errors

* Fix stream ready flow

* move all mqttparsing into session

* move packet parsing out of session

* Correct queue usage

* forgot the else

* Retry logic complete (for now)

* don’t reuse time intervals

* Add DatedSnapShot

* message id change

* DataSnapshot sendAll

* fix dated snapshot

* fix dated snapshot again

* Allow data snapshot to convert messages

* Fix read loop

* Swift 4 compatible with 3 changes.

* Add CustomStringConvertable to message, prototype file payload enum, make more structs

* cleanup lifecycle

* Break up networkPacket method

* move stream packet logic into Streamable

* bug fix

* Update

* better session handling

* Update .travis.yml

* Add swift_version in podspec

* Bump podspec versions

* Fix typo

* Keep payload as Data to minimize changes to public interface

* Remove inline change comments

* Make keep alive timer optional

* Update deprecated string count

* Update deprecated timer method

* Update readme

* Cleanup whitespace

* Update Example project

* Fix typo

* Update tests to ensure connection is made in setUp before running tests

* Remove extraneous } from example project

* Ensure callbacks are handled on the main thread

* Remove new reconnecting session feature for now

* Cleanup error handling

* Add ping/heartbeat callback

* Make error Equatable

* Cleanup private/fileprivate

* General formatting cleanup

* Update tests and add end-to-end subscribe, publish, receive test

* Remove new reconnecting session feature for now

* Add description for errors

* Remove Broker class

* Don't dispatch to main thread as already on main by default

* Expose desired dispatch queue for delegate callbacks, default to main

* Fix bug with reading ping packets

* Call completion blocks on delegate thread

* Clear the delegate when deinit'd

* Include framework as project dependency

* Update closure error handling

* Use description instead of localizedDescription

* Update readme

* Add Swift Package Manager support
Latest commit 0978b7d Aug 17, 2018


MQTT Client

Build Status Version License


Create Session

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


mqttSession.connect { error in
    if error == .none {
    } else {


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


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


let json = ["key" : "value"]
let data = try! 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 {

Conform to MQTTSessionDelegate to receive messages

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



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

target 'MyApp' do
    pod 'SwiftMQTT'


github "aciidb0mb3r/SwiftMQTT"

Swift Package Manager

dependencies: [
    .package(url: "", from: "3.0.0")