I am encountering an issue while using MQTT over WebSockets. Specifically, when I attempt to subscribe a message using a topic, the “granted QoS” field in the SUBACK response is always empty. The issue arises in the following method:
func didReceive(_ reader: CocoaMQTTReader, suback: FrameSubAck)
The failure occurs at this line:
swift
guard topicsAndQos.count == suback.grantedQos.count else {
printWarning("UNEXPECTED SUBACK Received: \(suback)")
return
}
The error message indicates a mismatch between the topic count and the granted QoS count, which results in the following:
UNEXPECTED SUBACK Received: Mismatched counts between topics and granted QoS
Here’s the relevant code for connection, publish, and subscribe:
if mqttVersion == "3.1.1" {
let clientID = "CocoaMQTT-\(animal!)-" + String(ProcessInfo().processIdentifier)
let websocket = CocoaMQTTWebSocket(uri: "/mqtt")
mqtt = CocoaMQTT(clientID: clientID, host: defaultHost, port: 8443, socket: websocket)
let username = "solace-cloud-client"
mqtt!.username = username
mqtt!.password = "oc1h2uejbmurk5r7fjv296v07a"
mqtt!.keepAlive = 120
mqtt!.delegate = self
mqtt!.enableSSL = true
mqtt!.autoReconnect = true
mqtt!.autoReconnectTimeInterval = 1
mqtt!.cleanSession = true
}
mqtt.subscribe("topic1", qos: .qos2)
mqtt.publish("topic1", withString: "broker", qos: .qos2)
Could you please assist with identifying the root cause of the mismatch and how to resolve the issue?
Steps to Reproduce:
- Establish connection using the above configuration.
- Subscribe to the topic “topic1” with QoS 2.
- Publish a message to the topic “topic1” with QoS 2.
- Receive SUBACK response where granted QoS is empty.
Looking forward to your assistance.