Issue with MQTT Connection OVER websocket - Network Connection Lost Error for iOS Swift

Dear Solace Community Team,

I’m encountering an issue while trying to connect to the Solace broker over MQTT using CocoaMQTT. Specifically, the connection attempt is being dropped with the error:

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."

The logs indicate that the connection is being lost after initiating the SSL/TLS handshake with the broker at https://mr-connection-cighcgrbttl.messaging.solace.cloud:443/mqtt.

Details:

  • Protocol: wss:// (WebSocket Secure) for port 443, which is the expected secure WebSocket connection.
  • CocoaMQTT Version: [Provide version]
  • Broker URL: https://mr-connection-cighcgrbttl.messaging.solace.cloud:443/mqtt
  • Error Message: The network connection was lost (NSURLErrorDomain Code=-1005).
  • Trust Information: The logs show the trust being received, indicating that SSL handshake is occurring, but the connection still fails.
  • Other Observations:
    • The connection state changes from “connecting” to “disconnected.”
    • The issue persists even after switching networks (tried mobile hotspot and other networks).
    • The connection fails every time at the same point.

iOS swift code :

 let clientID = "CocoaMQTT-\(animal!)-" + String(ProcessInfo().processIdentifier)
            let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
            mqtt = CocoaMQTT(clientID: clientID, host: defaultHost, port: 443, socket: websocket)
            let username = "solace-cloud-client"
            mqtt!.username = username
            mqtt!.password = "oc1h2uejbmurk5r7fjv296v07a"
            mqtt!.willMessage = CocoaMQTTMessage(topic: "topic1", string: "dieout")
            mqtt!.keepAlive = 60
            mqtt!.delegate = self
            mqtt!.enableSSL = true

Hi there, I’m Solly AI, I generate responses to your questions using the power of the Solace Agent Mesh:

Please remember that AI technology can make mistakes so ensure that you read our Disclaimer.

AI Generated responses are a work in progress, your help in evaluating my response would be awesome! If my response makes sense, please mark it as a solution or give a :+1:. If my response doesn’t make sense for your topic, please react to it with a :shaking_face:.

Looking at the documentation, I can help explain the MQTT over WSS connection issue with CocoaMQTT:

The error occurs because you’re trying to use incorrect ports for MQTT over WebSocket Secure (WSS) connections. Here’s the reasoning:

  1. For Solace Cloud brokers, MQTT connections should use:

    • Port 8883 for MQTT over SSL/TLS (TCPS)
    • Port 8443 for MQTT over WebSocket Secure (WSS)
  2. The ports you’re trying to use (443 and 55443) are actually for:

    • Port 443 - SMF (Solace Message Format) over WSS
    • Port 55443 - SMF over TCPS
  3. When using CocoaMQTT with WebSockets for Solace Cloud, you need to:

    • Use port 8443 for WSS connections
    • Configure the WebSocket URI path as “/mqtt”
    • Enable SSL for secure connections

To resolve the NSURLErrorDomain Code=-1005 error, you should modify your connection settings to use port 8443 for WSS connections to the Solace Cloud broker.

Here are relevant sources for more information:

1 Like

Yeah problem resolved. thanks
To extend implementation when I try to subscribe message getting error
UNEXPECTED SUBACK Received: Mismatched counts between topics and granted QoS

granted QoS getting empty for iOS swift

I found the solution to fix the issue by publishing the message using the following line:

            mqtt!.publish(topic, withString: message, qos: .qos1, retained: true)