{"id":20037974,"url":"https://github.com/perfectlysoft/perfect-mosquitto","last_synced_at":"2025-09-07T02:44:22.146Z","repository":{"id":63920168,"uuid":"84605650","full_name":"PerfectlySoft/Perfect-Mosquitto","owner":"PerfectlySoft","description":"A Swift Class Wrapper of Perfect-libMosquitto, the MQTT client","archived":false,"fork":false,"pushed_at":"2019-12-06T20:22:48.000Z","size":50,"stargazers_count":23,"open_issues_count":1,"forks_count":8,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-09-05T21:17:12.594Z","etag":null,"topics":["message-queue","mosquitto","mqtt"],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/PerfectlySoft.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-10T22:23:29.000Z","updated_at":"2023-06-13T02:09:26.000Z","dependencies_parsed_at":"2023-01-14T14:00:49.882Z","dependency_job_id":null,"html_url":"https://github.com/PerfectlySoft/Perfect-Mosquitto","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/PerfectlySoft/Perfect-Mosquitto","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Mosquitto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Mosquitto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Mosquitto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Mosquitto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PerfectlySoft","download_url":"https://codeload.github.com/PerfectlySoft/Perfect-Mosquitto/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PerfectlySoft%2FPerfect-Mosquitto/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273989687,"owners_count":25203260,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-07T02:00:09.463Z","response_time":67,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["message-queue","mosquitto","mqtt"],"created_at":"2024-11-13T10:24:23.062Z","updated_at":"2025-09-07T02:44:22.102Z","avatar_url":"https://github.com/PerfectlySoft.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Perfect-Mosquitto [简体中文](README.zh_CN.md)\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"http://perfect.org/get-involved.html\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://perfect.org/assets/github/perfect_github_2_0_0.jpg\" alt=\"Get Involed with Perfect!\" width=\"854\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/PerfectlySoft/Perfect\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_1_Star.jpg\" alt=\"Star Perfect On Github\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"http://stackoverflow.com/questions/tagged/perfect\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/perfect_gh_button_2_SO.jpg\" alt=\"Stack Overflow\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"https://twitter.com/perfectlysoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_3_twit.jpg\" alt=\"Follow Perfect on Twitter\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"http://perfect.ly\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_4_slack.jpg\" alt=\"Join the Perfect Slack\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://developer.apple.com/swift/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Swift-4.2-orange.svg?style=flat\" alt=\"Swift 4.2\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://developer.apple.com/swift/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Platforms-OS%20X%20%7C%20Linux%20-lightgray.svg?style=flat\" alt=\"Platforms OS X | Linux\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://perfect.org/licensing.html\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/License-Apache-lightgrey.svg?style=flat\" alt=\"License Apache\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://twitter.com/PerfectlySoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Twitter-@PerfectlySoft-blue.svg?style=flat\" alt=\"PerfectlySoft Twitter\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://perfect.ly\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://perfect.ly/badge.svg\" alt=\"Slack Status\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\nThis project provides a Swift class wrapper of `mosquitto` client library which implements the MQTT protocol version 3.1 and 3.1.1.\n\nThis package builds with Swift Package Manager and is part of the [Perfect](https://github.com/PerfectlySoft/Perfect) project, however, it can work independently as a Server Side Swift component.\n\nEnsure you have installed and activated the latest Swift tool chain.\n\n## OS X Notes\n\n### Homebrew Installation\n\nThis project depends on mosquitto library. To install on mac OS, try command `brew`:\n\n```\n$ brew install mosquitto\n```\n\n**Note** \n\n- You may need to add a symbol link like this (assuming mosquitto lib version 1.6.3)\n\n``` bash\n$ cd /usr/local/include \u0026\u0026 ln -s ../Cellar/mosquitto/1.6.3/include/mosquitto.h \n```\n\n- Also add the mosquitto `pkgconfig` path into environments:\n\n``` bash\nexport PKG_CONFIG_PATH=\"/usr/local/Cellar/mosquitto/1.6.3/share/pkgconfig\"\n```\n\n## Linux Notes\n\nThis project depends on Ubuntu 16.04 library `libmosquitto-dev`:\n\n```\n$ sudo apt-get install -y libmosquitto-dev\n```\n\n## Quick Start\n\n### Library Open / Close\n\nBefore using any functions of Perfect Mosquitto, please initialize the function set by calling `Mosquitto.OpenLibrary()`. Also, `Mosquitto.CloseLibrary()` is highly recommended once program quitted.\n\n*Note* Both operations are not thread safe, so please perform the operation prior to any threads.\n\n### Initialize a Mosquitto Client\n\nMosquitto instance can be constructed by calling with or without any parameters. The simplest form can be:\n\n``` swift\nlet m = Mosquitto()\n```\n\nWhich means assigning a random client id to this new instance, while all messages and subscriptions will be cleared once disconnected.\n\nHowever, you can also assign it with a customized client id with instruction of keeping all messages and subscription by this specific name. This is useful for resuming work in case of connection loss.\n\n``` swift\nlet mosquitto = Mosquitto(id: \"myInstanceId\", cleanSession: false)\n```\n\n### Connect to a Message Broker\n\nA message broker is a server that implements MQTT protocol and serves all clients in terms of messaging - receiving messages from producer and dispatching them to message subscribers.\n\nAlthough connection to a message broker can be asynchronous, keeping alive or binding to a specific network address, api `connect()` can be as express as demo below - only a host name and a port (usually 1883) are required:\n\n``` swift\ntry moosquitto.connect(host: \"mybroker.com\", port: 1883)\n```\n\nAlthough the instance can `disconnect()` from a broker automatically when no longer uses the object, it is recommended to call this function explicitly for a better practice. Besides, a `reconnect()` function is available for the same instance.\n\n### Threading Model\n\n#### Start / Stop\nPerfect Mosquitto is flexible in dealing with threads. Clients can call `start()` to run the mosquitto thread in background, which will automatically execute message publishing / receiving without any extra operations in the main thread, i.e., the thread will do the actual sending after calling `publish()` and activate callbacks for incoming messages. If no longer running, you can also stop the service thread at any time by calling `stop()`\n\n``` swift\n// start the messaging thread as a background service, it will not block the main thread and will return immediately.\ntry mosquitto.start()\n\n// do your other work in the main thread, such as publishing etc., and the messages received will go to the callbacks\n\n// stop the background messaging service if no longer need.\ntry mosquitto.stop()\n```\n\n#### Wait for Event\nOr, alternatively, you can process events in the main thread, by calling `wait()` method frequently in a message polling style:\n\n``` swift\n// wait a minimal while for events. \n// In this specific moment, mosquitto will perform actual message sending,\n// and pull messages from broker / run call backs.\ntry mosquitto.wait(0)\n```\n\nThe only parameter of `wait()` is the timeout value for message polling in milliseconds. Zero represents the minimal period of the system wait and negative value will be treated the same as 1000 (1 second).\n\n⚠️ *NOTE* ⚠️ *DO NOT MIX `start()`/`stop()` with `wait()`*\n\n\n### Publish a Message\n\nOnce connected to a broker, you can send messages at any time you want:\n\n``` swift\nvar msg = Mosquitto.Message()\nmsg.id = 100 // input your message id here\nmsg.topic = \"publish/test\"\nmsg.string = \"publication test 🇨🇳🇨🇦\"\n\nlet mid = try mosquitto.publish(message: msg)\n```\nAs demo above, firstly setup an empty message structure, then assign an message id (integer), a topic for this message and the message content; Then call `publish()` method to send it to the broker with a returned message id. \n\n*Note* Message content can also be a binary buffer, for example:\n``` swift\n// send a [Int8] array\nmsg.payload = [40, 41, 42, 43, 44, 45]\n```\n\nOnce published, call `start()` or `wait()` to perform the actual message sending as described in the Thread Model.\n\n### Message Subscription and Receiving\n\nThe only way to receive a MQTT message in Perfect Mosquitto is messaging callback:\n\n``` swift\n\nmosquitto.OnMessage = { msg in\n\n\t// print out message id\n\tprint(msg.id)\n\n\t// print out message topic\n\tprint(msg.topic)\n\n\t// print out message content\n\tprint(msg.string)\n\n\t// print out message body, in a binary array form\n\tprint(msg.payload)\n}//end on Message\n\n```\n\nOnce set the callback, you can call `subscribe()` to complete the message subscription on client side:\n\n``` swift\ntry mosquitto.subscribe(topic: \"publish/test\")\n```\n\nOnce subscribed, call `start()` or `wait()` to perform actual receiving process as described in the Thread Model.\n\n## More API\n\nPerfect Mosquitto also provides a rich set of functions beside the above ones, please check the project references for detail information.\n\n### Event Callbacks\n\nPlease set the following event callbacks for your mosquitto objects if need:\n\nAPI|Parameters|Description\n---|----------|-----------\n`OnConnect { status in }` | `ConnectionStatus` | Triggered on connection\n`OnDisconnected { status in }` | `ConnectionStatus` | Triggered on disconnection\n`OnPublish { msg in }` | `Message` | Triggered when message sent\n`OnMessage { msg in }` | `Message` | Triggered on message arrival\n`OnSubscribe { id, qos in }` | `(Int32, [Int32])` | Triggered on subscription\n`OnUnsubscribe { id in }` | `Int32` (message id) | Triggered on unsubscription\n`OnLog { level, content in }` | `(LogLevel, String)` | Triggered on log output\n\n### TLS Configuration\n\n- Set TLS certification file: `func setTLS(caFile: String?, caPath: String?, certFile: String? = nil, keyFile: String? = nil, keyPass: String? = nil) throws`\n\n- Set TLS verification method: `func setTLS(verify: SSLVerify = .PEER, version: String? = nil, ciphers: String? = nil) throws`\n\n- Set pre shared key: `func setTLS(psk: String, identity: String, ciphers: String? = nil) throws`\n\n### Miscellaneous Functions\n\n- Configure will information for a mosquitto instance: `func setConfigWill(message: Message?) throws`\n\n- Configure username and password for a mosquitton instance: `func login(username: String? = nil, password: String? = nil) throws`\n\n- Set the number of seconds to wait before retrying messages: `func setMessageRetry(max: UInt32 = 20)`\n\n- Set the number of QoS 1 and 2 messages that can be “in flight” at one time: `func setInflightMessages(max: UInt32 = 20) throws`\n\n- Control the behaviour of the client when it has unexpectedly disconnected: `func reconnectSetDelay(delay: UInt32 = 2, delayMax: UInt32 = 10, backOff: Bool = false) throws`\n\n- Reconnecting to a broker after a connection has been lost: `func reconnect(_ asynchronous: Bool = true) throws`\n\n- Reuse an existing mosquitto instance: `func reset(id: String? = nil, cleanSession: Bool = true) throws`\n\n- Set MQTT Version: `func setClientOption(_ version: MQTTVersion = .V31, value: UnsafeMutableRawPointer) throws`\n\n- Explain an exception (in English): `static func Explain(_ fault: Exception) -\u003e String`\n\n## Further Information\nFor more information on the Perfect project, please visit [perfect.org](http://perfect.org).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectlysoft%2Fperfect-mosquitto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fperfectlysoft%2Fperfect-mosquitto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fperfectlysoft%2Fperfect-mosquitto/lists"}