Skip to content

Commit 2fa8624

Browse files
authored
Update README.md
1 parent 79f9194 commit 2fa8624

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,27 @@ let publish = MQTTPublishInfo(
4343
)
4444
try client.publish(info: publish).wait()
4545
```
46-
## WebSockets and SSL
46+
## TLS
4747

48-
There is support for WebSockets and TLS connections. You can enable these through the `Configuration` provided at initialization. For TLS connections set`Configuration.useSSL` to `true` and provide your SSL certificates via the `Configuration.tlsConfiguration` struct. For WebSockets set `Configuration.useWebSockets` to `true` and set the URL path in `Configuration.webSocketsURLPath`.
48+
MQTT NIO supports TLS connections. You can enable these 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 certificate and your own certificate. They provide details on the website [https://test.mosquitto.org/](https://test.mosquitto.org/) on how to generate your own certificates.
49+
50+
```swift
51+
let rootCertificate = try NIOSSLCertificate.fromPEMBytes([UInt8](mosquittoCertificateText.utf8))
52+
let myCertificate = try NIOSSLCertificate.fromPEMBytes([UInt8](myCertificateText.utf8))
53+
let myPrivateKey = try NIOSSLPrivateKey(bytes: [UInt8](myPrivateKeyText.utf8), format: .pem)
54+
let tlsConfiguration: TLSConfiguration? = TLSConfiguration.forClient(
55+
trustRoots: .certificates(rootCertificate),
56+
certificateChain: myCertificate.map { .certificate($0) },
57+
privateKey: .privateKey(myPrivateKey)
58+
)
59+
let client = MQTTClient(
60+
host: "test.mosquitto.org",
61+
port: 8884,
62+
eventLoopGroupProvider: .createNew,
63+
configuration: .init(useSSL: true, tlsConfiguration: tlsConfiguration),
64+
)
65+
```
66+
67+
## WebSockets
68+
69+
MQTT also supports Web Socket connections. Set `Configuration.useWebSockets` to `true` and set the URL path in `Configuration.webSocketsURLPath` to enable these.

0 commit comments

Comments
 (0)