Skip to content

Commit

Permalink
Update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Adolfo Martinelli committed Apr 21, 2018
1 parent 576d657 commit 46e026a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 31 deletions.
89 changes: 58 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,76 +1,97 @@
# SwiftMQTT

MQTT Client in Swift
MQTT Client

[![Build Status](https://travis-ci.org/aciidb0mb3r/SwiftMQTT.svg)](https://travis-ci.org/aciidb0mb3r/SwiftMQTT)
[![Version](https://img.shields.io/cocoapods/v/SwiftMQTT.svg?style=flat)](http://cocoapods.org/pods/SwiftMQTT)
[![License](https://img.shields.io/cocoapods/l/SwiftMQTT.svg?style=flat)](http://cocoapods.org/pods/SwiftMQTT)

## Info
* Fully written in Swift
* Fully written in Swift 4
* Robust error handling
* Reconnection logic
* Performant
* Based on [MQTT 3.1.1 Specification](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html)

![Sample Project Screenshot](http://i.imgur.com/9lefVmVl.png)

## How to use
## Usage

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

### Connect
```swift
mqttSession.connect { (succeeded, error) -> Void in
if succeeded {
print("Connected!")
}
mqttSession.connect { error in
if error == .none {
print("Connected!")
} else {
print(error.description)
}
}
```

### Subscribe
```swift
mqttSession.subscribe(to: "/hey/cool", delivering: .atLeastOnce) { (succeeded, error) -> Void in
if succeeded {
print("Subscribed!")
}
let topic = "mytopic"
mqttSession.subscribe(to: topic, delivering: .atLeastOnce) { error in
if error == .none {
print("Subscribed to \(topic)!")
} else {
print(error.description)
}
}
```

### Unsubscribe
```swift
mqttSession.unSubscribe(from: ["/ok/cool", "/no/ok"]) { (succeeded, error) -> Void in
if succeeded {
print("unSubscribed!")
}
let topic = "mytopic"
mqttSession.unSubscribe(from: topic) { error in
if error == .none {
print("Unsubscribed from \(topic)!")
} else {
print(error.description)
}
}
```

### Publish
```swift
let jsonDict = ["hey" : "sup"]
let data = try! JSONSerialization.data(withJSONObject: jsonDict, options: .prettyPrinted)

mqttSession.publish(data, in: "/hey/wassap", delivering: .atLeastOnce, retain: false) { (succeeded, error) -> Void in
if succeeded {
print("Published!")
}
```swift
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
```swift
mqttSession.delegate = self
```
```swift
func mqttDidReceive(message: MQTTMessage, from session: MQTTSession) {
let string = message.stringRepresentation
print(message.stringRepresentation)
}
```
```swift
func mqttDidDisconnect(session: MQTTSession, reson: MQTTSessionDisconnect, error: Error?) {
func mqttDidDisconnect(session: MQTTSession, error: MQTTSessionError) {
if error == .none {
print("Successfully disconnected from MQTT broker")
} else {
print(error.description)
}
}
```

Expand All @@ -82,8 +103,14 @@ Install using [CocoaPods](http://cocoapods.org) by adding the following lines to

````ruby
use_frameworks!
pod 'SwiftMQTT', :git => 'https://github.com/oci-pronghorn/SwiftMQTT'
pod 'SwiftMQTT'
````

### Carthage

```
github "aciidb0mb3r/SwiftMQTT"
```

## License
MIT
Binary file removed SwiftMQTTExample/SwiftMQTTExample.sketch
Binary file not shown.

0 comments on commit 46e026a

Please sign in to comment.