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()
1 Like

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

Were you ever able to get a Swift HiveMQ example working with Swift? I’ve previously written an app to work with mosquitto on a Raspberry Pi, but I’d like to use HiveMQ as the broker. I can get HiveMQ to send messages to a Raspberry Pi Pico W microcontroller using the web client, but before I begin the work to try to create an MQTT5 client app in Swift, I’d like to confirm someone actually got this done before using CocoaMQTT (or other package). Thanks for your advice!

Hi @gallaugher @mgs227 @Daria_H
Were you able to get a Swift HiveMQ example working? Please confirm before I invest time implementing CocoaMQTT. If not, could you suggest alternative libraries that have worked seamlessly?

I was able to get a swift app - running CocoaMQTT - to work, but connecting to a generic MQTT 5.0 server. I have not tried my app connecting to HiveMQ. Not sure if this helps you or not.

Hello @mgs227 ,

Thank you for the follow-up!

Reviewing some of the documentation for CocoaMQTT, this sounds like it will be able to connect to HiveMQ instances without issue, as there is support for TLS connectivity.

It also appears that, in their included examples, they do offer a selection that provides an example of TLS implementation.

I would note that this is required for HiveMQ Cloud connectivity. Cloud Starter users can set up mTLS directly via the Access Management portal of their cloud instance, which can be used to provide the required certs for this library. Otherwise, one-sided TLS authentication can be enabled by ensuring the HiveMQ Server cert (available here) is trusted, or by trusting currently untrusted certificates.

mqtt.allowUntrustCACertificate = true

Best,
Aaron from the HiveMQ Team