Mqtt

0.3.0

Pure swift MQTT client
HJianBo/Mqtt

What's New

0.3.0

2018-02-10T11:36:01Z

Swift mqtt client for ios or osx 【中文介绍

Feature

  • Base Connect Subscribe Publish Unsubscribe Ping Disconnect method.
  • Clourse-style、asynchronous interface
  • Keep alive
  • Clean/Recover session state, when connected server
  • Support Linux (Ubuntu)

Requirements

  • iOS 8.0+ | macOS 10.10+ | tvOS 9.0+ | watchOS 2.0+
  • Xcode 9.x
  • Swift 4

Integration

Swift Packet Manager

Add this dependencies in the Package.swift

// swift-tools-version:4.0
import PackageDescription

let package = Package(
    // ...
    dependencies: [
        .package(url: "https://github.com/HJianBo/Mqtt", from: "0.2.0"),
    ],
    // ...
)

Carthage

Put the following line to your Cartfile

github "HJianBo/Mqtt"

Then checkout this repos

carthage build

But it will occur compile error because the Mqtt has some others dependencies with Swift Package Manager

There is a workaround as the following:

# Manually checkout SwiftPM dependencies
cd Carthage/Checkouts/Mqtt/ && swift build && cd ../../..

# execute carthage build again
carthage build

CocoaPods

come soon..

Usage

first, you should import the framework in .swift file header

import Mqtt

Create a client

let client = MqttClient(clientId: "mqtt-client-t")

Connect to server

client.connect(host: "q.emqtt.com") { address, error in
    guard error == nil else {
        print("connect failed, error: \(error)")
        return
    }
    print("connect successful! address: \(address)")
}

Subscribe topic with topic filters

client.subscribe(topicFilters: ["topic": .qos1]) { (res, error) in 
    guard error == nil else {
        print("subscribe topic error: \(error)")
        return
    }
    print("subscribe successful! topic authorized result: \(res)")
}

Publish message to some topic

client.publish(topic: "topic", payload: "hi~🙄", qos: .qos2) { error in
    guard error == nil else {
        print("publish message error: \(error)")
        return
    }
    print("publish message successful!")
}

The more details of the use can refer to the Demo program: Examples/SimpleClient

TODO

  • In-flight Send Window, Message Ordering.
  • Support iOS background model (will disconnect, when lock screen)
  • SSL/TLS

Dependencies

Description

  • Swift Tools 4.0.0
View More Packages from this Author

Dependencies

Last updated: Wed Mar 20 2024 13:14:18 GMT-0900 (Hawaii-Aleutian Daylight Time)