HiveMQ

iOS Swift Cloud HiveMQ example

Does anyone have an example of a iOS Swift application connecting to HiveMQ Cloud?

I am unable to connect -
When using MQTT v3.x - I got an error code of 5. (Not Authorized)
When using MQTT v5.x - I got an error code of 132 (Not Authorized)

I am trying to figure out the flags needed/not needed to connect via port 8883 - without the server certificate - similar to the mosquito-sub and mosquito-pub client apps., which both run on a mac without the need to specify the server.pem file

This is with CocoaMQTT:

Here is the code snippet that I was trying:

        var mqtt: CocoaMQTT5!
       
        let clientID = "CocoaMQTT-" + String(ProcessInfo().processIdentifier)
        let authProperties = MqttAuthProperties()
        let sslSettings: [String: NSObject] = [:]
        let server = "{YOUR INSTANCE}.s1.eu.hivemq.cloud"

        mqtt = CocoaMQTT5(clientID: clientID, host: server, port: 8883)
        mqtt.username = "{YOUR USERNAME}"
        mqtt.password = "{YOUR PASSWORD}"
        mqtt.willMessage = CocoaMQTT5Message(topic: "{YOUR TOPIC}", string: "{YOUR MESSAGE}")
        
        mqtt.keepAlive = 60
        mqtt.autoReconnect = true
        mqtt.enableSSL = true
        mqtt.allowUntrustCACertificate = true
        
                
        mqtt.sslSettings = sslSettings
        mqtt.auth(reasonCode: CocoaMQTTAUTHReasonCode.continueAuthentication, authProperties: authProperties)
        mqtt.connect()

Hi Michael,

Thanks for your interest in MQTT and HiveMQ! Unfortunately I do not have a Swift example at hand.
To troubleshoot the issue, first please confirm, if you can connect your cloud broker from a known MQTT Client. We suggest using the MQTT CLI:

#start MQTT CLI in Shell Mode:
mqtt sh
#connect to the cloud broker:
con -h ${YOUR INSTANCE}.s1.eu.hivemq.cloud -p 8883 -s -u ${YOUR USER} -pw ${YOUR PASSWORD}

(To get an overview of communication between the client and the broker, you can use Wireshark with a capture filter tcp.port in {8883})

Please confirm if you connect your cloud broker with MQTT CLI.

Thanks ,
Dasha from HiveMQ team

I am able to connect to HiveMq Cloud - using port 8883, no certificate, using mosquitto_sub and mosquitto_pub from a mac computer.

I cannot connect from a swift application.

Hi Michael,

Thank you for confirming this. One quick question: did you try your code with mqtt.allowUntrustCACertificate = false? Is the error any different?

Thanks,
Dasha from HiveMQ team

Here are the parameters I used, and the results:


mqtt.autoReconnect = true
mqtt.enableSSL = true
mqtt.allowUntrustCACertificate = false

State Change: connecting
135
State Change: disconnected

Then I tried:

        mqtt.autoReconnect = true
        mqtt.enableSSL = false
        mqtt.allowUntrustCACertificate = false

State Change: connecting
State Change: disconnected

The disconnect happens right away

Then I tried:

        mqtt.autoReconnect = true
        mqtt.enableSSL = true
        mqtt.allowUntrustCACertificate = true

State Change: connecting
State Change: disconnected

The disconnect takes about 1 min to show up

Hi Michael,

Thank you for checking. I have also tested the CocoaMQTT library’s simpleSSL connection to the HiveMQ broker and it does not work, while the HiveMQ MQTT Client library works well. There might be something wrong (or outdated) with CocoaMQTT implementation of the simple SSL.
Unfortunately we do not have a Swift library of our own. I am sorry that I could not help you more.

Kind regards,
Dasha from HiveMQ team

1 Like