https://github.com/s2ler/smosquitto
Lightweight Swift wrapper around libmosquitto client library.
https://github.com/s2ler/smosquitto
iot mosquitto mqtt mqtt-client swift swift-wrapper
Last synced: 4 months ago
JSON representation
Lightweight Swift wrapper around libmosquitto client library.
- Host: GitHub
- URL: https://github.com/s2ler/smosquitto
- Owner: S2Ler
- License: mit
- Created: 2019-01-07T10:34:53.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-10-11T20:41:19.000Z (over 4 years ago)
- Last Synced: 2026-01-26T08:33:56.161Z (5 months ago)
- Topics: iot, mosquitto, mqtt, mqtt-client, swift, swift-wrapper
- Language: Swift
- Homepage:
- Size: 78.1 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SMoquitto
Swift 5.0, macOS, Linux.
A thin Swift wrapper for [libmosquitto](https://github.com/eclipse/mosquitto/tree/master/lib). Supports macOS and Linux.
## Installation
Use [SPM](https://swift.org/package-manager/).
Sample `Package.swift`
```swift
// swift-tools-version:5.0
import PackageDescription
let package = Package(
name: "ProjectName",
products: [
.executable(name: "ProjectName", targets: ["ProjectName"]),
],
dependencies: [
.package(url: "https://github.com/diejmon/SMosquitto.git", .upToNextMinor(from: "1.4.0")),
],
targets: [
.target(
name: "ProjectName",
dependencies: ["SMosquitto"]),
],
swiftLanguageVersions: [.v5]
)
```
## Usage
```swift
import SMosquitto
SMosquitto.initialize()
let smosquitto = SMosquitto(id: "An ID", cleanSession: true)
try smosquitto.setLoginInformation(username: "username", password: "password")
try smosquitto.connect(host: "hostname", port: 1883, keepalive: true)
smosquitto.onConnect = { response in
do {
// Subscribe for topics once connected to server.
try smosquitto.subscribe(subscriptionPattern: "atopic", qos: .atleastOnce)
}
catch {
// an error happened
}
}
smosquitto.onMessage = { message in
// Do something with received message
}
/// Start async loop
try smosquitto.loopForever(timeout: .interval(45))
```
## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.