Skip to content

Commit 8f72844

Browse files
authored
Add Async/Await section to readme
1 parent 4601dec commit 8f72844

File tree

1 file changed

+57
-11
lines changed

1 file changed

+57
-11
lines changed

README.md

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@ let client = MQTTClient(
1818
identifier: "My Client",
1919
eventLoopGroupProvider: .createNew
2020
)
21-
try client.connect().wait()
21+
client.connect().whenComplete { result in
22+
switch result {
23+
case .success:
24+
print("Succesfully connected")
25+
case .failure(let error):
26+
print("Error while connecting \(error)")
27+
}
28+
}
2229
```
2330

24-
Subscribe to a topic and add a publish listener to report publish messages from the broker.
31+
Subscribe to a topic and add a publish listener to report publish messages sent from the server/broker.
2532
```swift
26-
let subscription = MQTTSubscribeInfo(
27-
topicFilter: "my-topics",
28-
qos: .atLeastOnce
29-
)
30-
try client.subscribe(to: [subscription]).wait()
33+
let subscription = MQTTSubscribeInfo(topicFilter: "my-topics", qos: .atLeastOnce)
34+
client.subscribe(to: [subscription]).whenComplete { result in
35+
...
36+
}
3137
client.addPublishListener("My Listener") { result in
3238
switch result {
3339
case .success(let publish):
@@ -42,13 +48,53 @@ client.addPublishListener("My Listener") { result in
4248

4349
Publish to a topic.
4450
```swift
45-
let payload = ByteBufferAllocator().buffer(string: "This is the Test payload")
46-
try client.publish(
51+
client.publish(
4752
to: "my-topics",
48-
payload: payload,
53+
payload: ByteBuffer(string: "This is the Test payload"),
4954
qos: .atLeastOnce
50-
).wait()
55+
).whenComplete { result in
56+
...
57+
}
5158
```
59+
Each MQTTClient function returns a Swift NIO `EventLoopFuture`. In the examples above I just use `whenComplete` to process the results, but you can use various methods to chain `EventLoopFuture` together. You can find out more about Swift NIO [here](https://apple.github.io/swift-nio/docs/current/NIO/Classes/EventLoopFuture.html).
60+
61+
## Swift Concurrency
62+
63+
Alongside the `EventLoopFuture` APIs MQTTNIO also includes async/await versions. So where with `EventLoopFutures` you would write
64+
```swift
65+
client.connect()
66+
.flatMap { _ -> EventLoopFuture<MQTTSuback> in
67+
let subscription = MQTTSubscribeInfo(topicFilter: "my-topics", qos: .atLeastOnce)
68+
return client.subscribe(to: [subscription])
69+
}
70+
.whenComplete { result in
71+
doStuff()
72+
}
73+
```
74+
you can now replace it with
75+
```swift
76+
_ = try await client.connect()
77+
let subscription = MQTTSubscribeInfo(topicFilter: "my-topics", qos: .atLeastOnce)
78+
_ = try await client.subscribe(to: [subscription])
79+
doStuff()
80+
```
81+
82+
### PUBLISH listener AsyncSequence
83+
If you don't want to parse incoming PUBLISH packets via a callback the Swift concurrency support also includes an `AsyncSequence` for this purpose.
84+
```swift
85+
let listener = client.createPublishListener()
86+
for await result in listener {
87+
switch result {
88+
case .success(let publish):
89+
var buffer = publish.payload
90+
let string = buffer.readString(length: buffer.readableBytes)
91+
print(string)
92+
case .failure(let error):
93+
print("Error while receiving PUBLISH event")
94+
}
95+
}
96+
```
97+
5298
## TLS
5399

54100
MQTT NIO supports TLS connections. You can enable this through the `Configuration` provided at initialization. Set`Configuration.useSSL` to `true` and provide your SSL certificates via the `Configuration.tlsConfiguration` struct. For example to connect to the mosquitto test server `test.mosquitto.org` on port 8884 you need to provide their root certificate and your own certificate. They provide details on the website [https://test.mosquitto.org/](https://test.mosquitto.org/) on how to generate these.

0 commit comments

Comments
 (0)