Ankit Patel
Ankit Patel

Reputation: 39

Having problem connecting to my mqtt broker using CocoaMQTT

I am making one iOS app communicating with Mqtt broker, mainly to publish message. But when I try to connect with broker using CocoaMQTT library it's always giving me error in connection.

I am trying with CocoaMQTT latest version and also 1.1.3 version. But both are failing in connection and giving me error

(Error Domain=kCFStreamErrorDomainNetDB Code=8 "nodename nor servname provided, or not known" UserInfo={NSLocalizedDescription=nodename nor servname provided, or not known})

 formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS"
    let dateString = formatter.string(from: date)
    let clientID = "smart-curtain-"+dateString
mqttClient = CocoaMQTT.init(clientID: clientID, host: 
    contantData.MQTT_BROKER_URL, port: UInt16(1883))
        mqttClient.username = nil
        mqttClient.password = nil
        mqttClient.autoReconnect = true
        mqttClient.allowUntrustCACertificate = true
        mqttClient.keepAlive = 60
       mqttClient.enableSSL = false

So its always ending up withmqttDidDisconnect delegate method. My broker url is tcp://xyz.com (xyz is just example) and port is 1883. I have tried 2-3 Mqtt toll apps from my iPhone to connect with broker detail, but no one able to connect it.

But same settings working fine in Android app. (it is using net.igenius:mqttservice:1.6.4) (this broker is no need authentication)

Upvotes: 0

Views: 2899

Answers (2)

Sreekanth M
Sreekanth M

Reputation: 151

    var session = CocoaMQTT.init(clientID: "user1", host: "xx.xx.xxx.xx", port: 1883)
    session.allowUntrustCACertificate = true

No need to send tcp://xx.xx.xxx.xx:port as in android, You can just pass xx.xx.xxx.xx by removing tcp:// and port number separately.

Upvotes: 1

hardillb
hardillb

Reputation: 59816

As shown in the CocoaMQTT doc, the host entry in the init method should be just the hostname, not a URI:

let clientID = "CocoaMQTT-" + String(NSProcessInfo().processIdentifier)
let mqtt = CocoaMQTT(clientID: clientID, host: "localhost", port: 1883)
mqtt.username = "test"
mqtt.password = "public"
mqtt.willMessage = CocoaMQTTWill(topic: "/will", message: "dieout")
mqtt.keepAlive = 60
mqtt.delegate = self
mqtt.connect()

e.g. should be xyz.com not tcp://xyz.com

Upvotes: 3

Related Questions