Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide working example #570

Open
arikivandeput opened this issue Dec 18, 2023 · 0 comments
Open

provide working example #570

arikivandeput opened this issue Dec 18, 2023 · 0 comments

Comments

@arikivandeput
Copy link

please provide working example in swiftUI for setting up the client and process the ReceiveMessage
i keep getting errors

import Foundation
import CocoaMQTT
class MQTTManager {

static let shared = MQTTManager()
private init(){
	
}
func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
	if ack == .accept {
		mqtt.subscribe("bikes", qos: CocoaMQTTQoS.qos1)
	}
}

func setup(){
	
	let mqtt = CocoaMQTT5(clientID: "iOS_Client", host: "2.tcp.eu.ngrok.io", port: 13963)
 
	mqtt.connect()
	mqtt.didReceiveMessage = { mqtt, message, id in
		theReturnString = ("Message received in topic (message.topic) with payload (message.string!)")

// mqtt.didConnectAck = { mqtt, ack in
// print("Connected: (ack)")
// mqtt.subscribe("bikes")
// }
//
// mqtt.didSubscribeTopic = { mqtt, topic, qos in
// print("Subscribed to (topic) with QoS (qos)")
// }
//
// mqtt.didReceiveMessage = { mqtt, message, id in
// guard let string = message.string else {
// print("Received a message but couldn't parse the payload.")
// return
// }
//
// print("Message received in topic (message.topic) with payload (string)")
// }

//
// // The publish can be moved to a more appropriate place after ensuring connection is established
// let publishProperties = MqttPublishProperties()
// publishProperties.contentType = "JSON"
// mqtt.publish("bikes", withString: "from iOS", properties: publishProperties)
//

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant