I am trying to connect to MQTT broker, in very simple steps, but I am getting error CocoaMQTT(error): The deliver delegate is nil!!! the frame will be drop:PUBLISH(id: 2, topic: your-topic, payload: [72, 101, 108,...]
I have create one class as:
import CocoaMQTT
class MQTTManager: CocoaMQTTDelegate {
var mqtt: CocoaMQTT!
func mqtt(_ mqtt: CocoaMQTT, didPublishAck id: UInt16) {
print("Published message with ID: \(id)")
}
func mqtt(_ mqtt: CocoaMQTT, didSubscribeTopics success: NSDictionary, failed: [String]) { print("Subscribed to topics: \(success)") }
func mqtt(_ mqtt: CocoaMQTT, didUnsubscribeTopics topics: [String]) { print("Unsubscribed from topics: \(topics)") }
func mqttDidPing(_ mqtt: CocoaMQTT) { print("Pinged!") }
func mqttDidReceivePong(_ mqtt: CocoaMQTT) { print("Ponged!") }
func mqttDidDisconnect(_ mqtt: CocoaMQTT, withError err: (any Error)?) { print("Disconnected from the MQTT") }
func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
if ack == .accept {
print("Connected to the MQTT!")
} else {
print("Failed to connect to MQTT")
}
}
func mqtt(_ mqtt: CocoaMQTT, didPublishMessage message: CocoaMQTTMessage, id: UInt16) {
print("Data published successfully")
}
func mqtt(_ mqtt: CocoaMQTT, didReceiveMessage message: CocoaMQTTMessage, id: UInt16) {
if let messageString = message.string {
print("Received message on topic \(message.topic): \(messageString)")
}
}
func connectMQTT() {
mqtt = CocoaMQTT.init(clientID: "your-client-id-435345", host: "your-client-id-435345", port: 1883)
//tried with CocoaMQTT(clientID: "your-client-id-435345", host: "your-client-id-435345", port: 1883)
mqtt.delegate = self
mqtt.connect()
}
func subscribeToTopic(topic: String) {
mqtt.subscribe(topic)
}
func publishData(topic: String, message: String) {
mqtt.publish(topic, withString: message, qos: .qos1)
}
}
I am trying to use it in function as:
func sendTelemetryMsg(password: String, url: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
let mqttManager = MQTTManager();
mqttManager.connectMQTT()
// Subscribe to a topic
let topic = "your-topic"
mqttManager.subscribeToTopic(topic: topic)
// Publish data to the IoT device
let message = "Hello, IoT Device!"
mqttManager.publishData(topic: topic, message: message)
}
I recommend that you seek help via the support channel for the third-party library you’re using. DevForums is primarily focused on Apple technologies. It’s possible that someone familiar with that API might see your question here, but you’re more likely to find those folks elsewhere.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"