Podfile file:target 'YourTarget' dopod 'CocoaMQTT', '~> 2.1.6'end
pod install
CocoaMQTT for 3.1.1 and CocoaMQTT5 for 5.0).https://github.com/emqx/CocoaMQTT.gitimport CocoaMQTTCartfile file:github "emqx/CocoaMQTT" "master"
carthage update --platform iOS,macOS,tvOS --use-xcframeworks
.xcframework into the project and set it to "Embed & Sign".CocoaMQTT5), please note the following key points:didConnectAck receives 3 parameters.mqtt.didConnectAck = { mqtt, ack, properties in// ack is of CocoaMQTTCONNACKReasonCode type// properties is of MqttDecodeConnAck? type}
.success instead of .acceptif ack == .success { // Correct// Connection successful}
MqttSubscription objects.let subscriptions = [MqttSubscription(topic: "home/test", qos: .qos1)]mqtt.subscribe(subscriptions)
[UInt8] type.mqtt.didReceiveMessage = { mqtt, message, id, properties in// message.payload is of [UInt8] type and needs to be convertedlet content = String(data: Data(message.payload), encoding: .utf8) ?? ""}
properties parameter.mqtt.publish(topic, withString: message, qos: .qos1, properties: MqttPublishProperties())
mqtt.didPublishAck = { mqtt, id, properties inprint("Publish acknowledgement - ID: \\(id)")}
mqtt.didSubscribeTopics = { mqtt, success, failed, properties in// success is of type [(String, CocoaMQTTQoS)]}
import CocoaMQTTclass MQTTManager {var mqtt: CocoaMQTT5!func setupMQTT() {// Get the access point from the MQTT console:// For users who implement VPC network integration via Private Link, use private network access point;// For users accessing over the public network, ensure the public network security policy permits access, and the machine running the program has public network connectivity;let host = "mqtt-xxx.mqtt.tencenttdmq.com"let port: UInt16 = 1883// A valid Client Identifier contains digits 0-9, lowercase English letters a-z, and uppercase English letters A-Z, with a total length of 1-23 characters// See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901059let clientID = "QuickStart"mqtt = CocoaMQTT5(clientID: clientID, host: host, port: port)// On the console --> Create account on the Authentication Tab, copy username and passwordmqtt.username = "YOUR_USERNAME"mqtt.password = "YOUR_PASSWORD"mqtt.keepAlive = 60mqtt.cleanSession = truemqtt.autoReconnect = true// Set the callback.mqtt.didConnectAck = { mqtt, ack, _ inprint("Connection response: \\(ack)")if ack == .success {// MQTT topiclet pubTopic = "home/test"// Subscribe to a topic.let subscriptions = [MqttSubscription(topic: pubTopic, qos: .qos1),MqttSubscription(topic: "home/#", qos: .qos1),MqttSubscription(topic: "home/+", qos: .qos1)]mqtt.subscribe(subscriptions)print("Subscribed to the topic")}}mqtt.didPublishMessage = { mqtt, message, id inprint("Message published successfully - ID: \\(id)")}mqtt.didPublishAck = { mqtt, id, _ inprint("Received publish acknowledgement - ID: \\(id)")}mqtt.didReceiveMessage = { mqtt, message, _, _ inlet content = String(data: Data(message.payload), encoding: .utf8) ?? ""print("Received message - Topic: \\(message.topic), QoS: \\(message.qos.rawValue), Content: [\\(content)]")}mqtt.didSubscribeTopics = { mqtt, success, failed, _ inprint("Subscription successful: \\(success)")if !failed.isEmpty {print("Subscription failed: \\(failed)")}}mqtt.didDisconnect = { mqtt, error inif let error = error {print("Disconnected: \\(error.localizedDescription)")} else {print("Disconnected")}}// Connect to the server_ = mqtt.connect()}func publishMessages() {let pubTopic = "home/test"let total = 16for i in 0..<total {let message = "Hello MQTT \\(i)"print("Preparing to publish message \\(i)")mqtt.publish(pubTopic, withString: message, qos: .qos1, properties: MqttPublishProperties())print("Message published \\(i)")// Delay 3 secondsThread.sleep(forTimeInterval: 3)}// Disconnect after waiting for 3 secondsThread.sleep(forTimeInterval: 3)mqtt.disconnect()}}
import CocoaMQTTclass MQTTManagerTLS {var mqtt: CocoaMQTT5!func setupMQTT() {// Get the access point from the MQTT console:// For users who implement VPC network integration via Private Link, use private network access point;// For users accessing over the public network, ensure the public network security policy permits access, and the machine running the program has public network connectivity;let host = "mqtt-xxx.mqtt.tencenttdmq.com"let port: UInt16 = 8883// A valid Client Identifier contains digits 0-9, lowercase English letters a-z, and uppercase English letters A-Z, with a total length of 1-23 characters// See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901059let clientID = "QuickStartTls"mqtt = CocoaMQTT5(clientID: clientID, host: host, port: port)// On the console --> Create account on the Authentication Tab, copy username and passwordmqtt.username = "YOUR_USERNAME"mqtt.password = "YOUR_PASSWORD"mqtt.keepAlive = 60mqtt.cleanSession = truemqtt.autoReconnect = true// Enable SSL/TLSmqtt.enableSSL = truemqtt.allowUntrustCACertificate = false// Set the callback.mqtt.didConnectAck = { mqtt, ack, _ inprint("Connection response: \\(ack)")if ack == .success {// MQTT topiclet topicName = "home/test"// Subscribe to a topic.let subscriptions = [MqttSubscription(topic: topicName, qos: .qos1),MqttSubscription(topic: "home/#", qos: .qos1),MqttSubscription(topic: "home/+", qos: .qos1)]mqtt.subscribe(subscriptions)}}mqtt.didPublishMessage = { mqtt, message, id inprint("Message published successfully - ID: \\(id)")}mqtt.didPublishAck = { mqtt, id, _ inprint("Received publish acknowledgement - ID: \\(id)")}mqtt.didReceiveMessage = { mqtt, message, _, _ inlet content = String(data: Data(message.payload), encoding: .utf8) ?? ""print("Received message - Topic: \\(message.topic), QoS: \\(message.qos.rawValue), Content: [\\(content)]")}mqtt.didSubscribeTopics = { mqtt, success, failed, _ infor (topic, qos) in success {print("Subscription successful - Topic: \\(topic), Granted-QoS: \\(qos)")}if !failed.isEmpty {print("Subscription failed: \\(failed)")}}mqtt.didDisconnect = { mqtt, error inif let error = error {print("Disconnected: \\(error.localizedDescription)")} else {print("Disconnected")}}// Connect to the server_ = mqtt.connect()}func publishMessages() {let topicName = "home/test"let total = 16for i in 0..<total {let message = "Hello MQTT \\(i)"print("Preparing to publish message \\(i)")mqtt.publish(topicName, withString: message, qos: .qos1, properties: MqttPublishProperties())print("Message published \\(i)")// Delay 3 secondsThread.sleep(forTimeInterval: 3)}// Disconnect after waiting for 3 secondsThread.sleep(forTimeInterval: 3)mqtt.disconnect()}}
import CocoaMQTTclass MQTTManager311 {var mqtt: CocoaMQTT!func setupMQTT() {// Get the access point from the MQTT console:// For users who implement VPC network integration via Private Link, use private network access point;// For users accessing over the public network, ensure the public network security policy permits access, and the machine running the program has public network connectivity;let host = "mqtt-xxx.mqtt.tencenttdmq.com"let port: UInt16 = 1883// A valid Client Identifier contains digits 0-9, lowercase English letters a-z, and uppercase English letters A-Z, with a total length of 1-23 characters// See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901059let clientID = "QuickStart"mqtt = CocoaMQTT(clientID: clientID, host: host, port: port)// On the console --> Create account on the Authentication Tab, copy username and passwordmqtt.username = "YOUR_USERNAME"mqtt.password = "YOUR_PASSWORD"mqtt.keepAlive = 60mqtt.cleanSession = truemqtt.autoReconnect = true// Set proxymqtt.delegate = self// Connect to the server_ = mqtt.connect()}func publishMessages() {// Confirm that the first-level topic "home" has been created in the MQTT consolelet pubTopic = "home/test"let total = 16for i in 0..<total {let message = "Hello MQTT \\(i)"print("Preparing to publish message \\(i)")mqtt.publish(pubTopic, withString: message, qos: .qos1)print("Message published \\(i)")// Delay 3 secondsThread.sleep(forTimeInterval: 3)}// Disconnect after waiting for 3 secondsThread.sleep(forTimeInterval: 3)mqtt.disconnect()}}// MARK: - CocoaMQTTDelegateextension MQTTManager311: CocoaMQTTDelegate {func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {print("Connection response: \\(ack)")if ack == .accept {// Confirm that the first-level topic "home" has been created in the MQTT consolelet pubTopic = "home/test"let topicFilters = [pubTopic, "home/#", "home/+"]// Subscribe to a topic.mqtt.subscribe(topicFilters, qos: [.qos1, .qos1, .qos1])print("Subscribed to \\(topicFilters.count) topics")}}func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) {print("Message published successfully - ID: \\(id)")}func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) {print("Received publish acknowledgement - Packet-ID: \\(id)")}func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {let content = message.string ?? ""print("Received message - Topic: \\(message.topic), QoS: \\(message.qos.rawValue), Dup: \\(message.duplicated), Retained: \\(message.retained), Content: [\\(content)]")}func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopics success: NSDictionary, failed: [String]) {print("Subscription successful: \\(success)")if !failed.isEmpty {print("Subscription failed: \\(failed)")}}func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopics topics: [String]) {print("Unsubscribe: \\(topics)")}func mqttDidPing(_ mqtt: CocoaMQTT) {// Heartbeat}func mqttDidReceivePong(_ mqtt: CocoaMQTT) {// Heartbeat response}func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {if let error = err {print("Disconnected: \\(error.localizedDescription)")} else {print("Disconnected")}}}
import CocoaMQTTclass MQTTManager311TLS {var mqtt: CocoaMQTT!func setupMQTT() {// Get the access point from the MQTT console:// For users who implement VPC network integration via Private Link, use private network access point;// For users accessing over the public network, ensure the public network security policy permits access, and the machine running the program has public network connectivity;let host = "mqtt-xxx.mqtt.tencenttdmq.com"let port: UInt16 = 8883// A valid Client Identifier contains digits 0-9, lowercase English letters a-z, and uppercase English letters A-Z, with a total length of 1-23 characters// See https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901059let clientID = "ClientQuickStartTls"mqtt = CocoaMQTT(clientID: clientID, host: host, port: port)// On the console --> Create account on the Authentication Tab, copy username and passwordmqtt.username = "YOUR_USERNAME"mqtt.password = "YOUR_PASSWORD"mqtt.keepAlive = 60mqtt.cleanSession = truemqtt.autoReconnect = true// Enable SSL/TLSmqtt.enableSSL = truemqtt.allowUntrustCACertificate = false// Set proxymqtt.delegate = self// Connect to the server_ = mqtt.connect()}func publishMessages() {// Confirm that the first-level topic "home" has been created in the MQTT consolelet topic = "home/test"let total = 16for i in 0..<total {let message = "Hello MQTT \\(i)"print("Preparing to publish message \\(i)")mqtt.publish(topic, withString: message, qos: .qos1)print("Message published \\(i)")// Delay 1 secondThread.sleep(forTimeInterval: 1)}// Disconnect after waiting for 3 secondsThread.sleep(forTimeInterval: 3)mqtt.disconnect()}}// MARK: - CocoaMQTTDelegateextension MQTTManager311TLS: CocoaMQTTDelegate {func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {print("Connection response: \\(ack)")if ack == .accept {let topicFilters = ["home/test"]// Subscribe to a topic.mqtt.subscribe(topicFilters, qos: [.qos1])print("Subscribed to \\(topicFilters.count) topics")}}func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) {// Message publishing completed}func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) {print("Received publish acknowledgement - Packet-ID: \\(id)")}func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {let content = message.string ?? ""print("Received message - Topic: \\(message.topic), QoS: \\(message.qos.rawValue), Content: [\\(content)]")}func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopics success: NSDictionary, failed: [String]) {print("Subscription successful: \\(success)")}func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopics topics: [String]) {print("Unsubscribe: \\(topics)")}func mqttDidPing(_ mqtt: CocoaMQTT) {// Heartbeat}func mqttDidReceivePong(_ mqtt: CocoaMQTT) {// Heartbeat response}func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: Error?) {if let error = err {print("Disconnected: \\(error.localizedDescription)")} else {print("Disconnected")}}}
Parameter | Description |
pubTopic | The target topic for the message to be published. Message topics can contain multiple levels, separated by '/'. The first level Topic needs to be created on the console Topic Tab page. For details, refer to Topic Names and Topic Filters. |
topicFilters | One or more subscription expressions. Subscription expressions can contain wildcards (Wildcards), for details, refer to Topic Names and Topic Filters. |
qos | QoS array, The array length must match the number of Topic Filters elements. The most commonly used QoS is 1, which is At-Least-Once delivery. For details, refer to Quality of Service levels and protocol flows. |
host | MQTT instance endpoint host address. In the console, under the target cluster Basic Information > Access Information module, copy it. The location is shown in the figure below. Standard access point: mqtt-xxx.mqtt.tencenttdmq.com (port 1883)TLS access point: mqtt-xxx.mqtt.tencenttdmq.com (port 8883)Standard WebSocket access point: mqtt-xxx.mqtt.tencenttdmq.com (port 80, path /mqtt)TLS WebSocket access point: mqtt-xxx.mqtt.tencenttdmq.com (port 443, path /mqtt) |
clientID | Device unique identifier: such as vehicle identification number (VIN), product serial number, and so on. A valid Client Identifier contains: digits 0-9, lowercase letters a-z, uppercase letters A-Z; with a total length of 1-23 characters. For details, refer to Client Identifier. |
username | Connection username can be copied from the Authentication Management page under Cluster Details in the console. |
password | The password matching the Connection Username can be copied from the Authentication Management page on the cluster details console. |
Feedback