You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+57-11Lines changed: 57 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,16 +18,22 @@ let client = MQTTClient(
18
18
identifier: "My Client",
19
19
eventLoopGroupProvider: .createNew
20
20
)
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
+
}
22
29
```
23
30
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.
25
32
```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
+
}
31
37
client.addPublishListener("My Listener") { result in
32
38
switch result {
33
39
case .success(let publish):
@@ -42,13 +48,53 @@ client.addPublishListener("My Listener") { result in
42
48
43
49
Publish to a topic.
44
50
```swift
45
-
let payload =ByteBufferAllocator().buffer(string: "This is the Test payload")
46
-
try client.publish(
51
+
client.publish(
47
52
to: "my-topics",
48
-
payload: payload,
53
+
payload: ByteBuffer(string: "This is the Test payload"),
49
54
qos: .atLeastOnce
50
-
).wait()
55
+
).whenComplete { result in
56
+
...
57
+
}
51
58
```
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
+
_=tryawait client.connect()
77
+
let subscription =MQTTSubscribeInfo(topicFilter: "my-topics", qos: .atLeastOnce)
78
+
_=tryawait 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
+
forawait 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
+
52
98
## TLS
53
99
54
100
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