Giter Site home page Giter Site logo

swiftmqtt's Introduction

SwiftMQTT

MQTT Client

Build Status Version License

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

CocoaPods

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

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

Carthage

github "aciidb0mb3r/SwiftMQTT"

Swift Package Manager

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

License

MIT

swiftmqtt's People

Contributors

aciidgh avatar achase55 avatar michael-airmap avatar adolfo avatar kervich avatar paulw11 avatar f2m2rd avatar

Stargazers

cdf1982 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.