MQTT-Client-Framework
MQTT-Client-Framework copied to clipboard
Cannot subscribe topic
When I successfully connected socket. I subscribe topic. Then I'm getting error and web socket closed.
session?.subscribe(toTopic: subscriptionTopic, at: MQTTQosLevel.exactlyOnce, subscribeHandler: { (err, gQoss) in
if (err != nil){
print("MQTTSessionx Subscription failed \(err)");
}else{
print("MQTTSessionx Subscription sucessfull! Granted Qos: \(gQoss)");
}
})
I'm getting this error :
MQTTSessionx Subscription failed Optional(Error Domain=MQTT Code=-6 "No response" UserInfo={NSLocalizedDescription=No response}) [SR] NSStreamEventHasSpaceAvailable <__NSCFOutputStream: 0x108118480> [MQTTSession] mqttTransport send [MQTTWebsocketTransport] send(67):<82410002 003c696e 646f6f72 2f706f73 6974696f 6e732f6c 61746573 742f6265 35386131 31622d31 3236652d 34373639 2d386538 652d6438 30623830 39353462 623602> [SR] NSStreamEventOpenCompleted <__NSCFOutputStream: 0x108045080> [SR] Closing with code 1000 reason (null) [SR] Trying to disconnect [SR] NSStreamEventHasSpaceAvailable <__NSCFOutputStream: 0x108045080>
The connection was closed by yourself or the broker before the SUBACK packet was received.
Which broker do you use? Can you subscribe with QoS Exactly Once for the topic you use with another client?
@yusufonderd What is your subscriptionTopic
here? Does it include device-id
?
@mendirattanishant My subscriptionTopic is custom string. It starts with 'indoor/position/..'
@yusufonderd and it works fine in Android client? In my case it was not connecting as I was not setting the clientId
correctly.
Yes it is worked fine android side. I was set clientId normally.
let clientID = "Indoor" + uniqueValue
let transport = MQTTWebsocketTransport()
transport.url = websocketUrl
self.session = MQTTSession(clientId: clientID)
self.session.transport = transport
self.session.delegate = self
self.session.connectAndWaitTimeout(5.0)
self.session.connect(connectHandler: { (err) in
if let error = err{
print("MQTTx err:\(error)")
}else{
print("MQTTx successfully connected")
session?.subscribe(toTopic: subscriptionTopic, at: MQTTQosLevel.exactlyOnce)
}
})
@yusufonderd can you just call connect()
method on session and then have your delegates method subscribe to topic.
Once there is a connection established, the delegate method
func handleEvent(_ session: MQTTSession!, event eventCode: MQTTSessionEvent, error: Error!)
would get called. In the delegate method, call subscribe()
to subscribe to the topics.
func handleEvent(_ session: MQTTSession!, event eventCode: MQTTSessionEvent, error: Error!) {
switch eventCode {
case .connected:
sessionConnected = true
session.subscribe(toTopic: subscriptionTopic, at: MQTTQosLevel.exactlyOnce)
case .connectionClosed:
sessionConnected = false
default:
sessionError = true
}
}
Reference: https://github.com/novastone-media/MQTT-Client-Framework/blob/master/MQTTSwift/MQTTSwift/MQTTSwift.swift
The delegate method will give you a better clarity if the connection is getting established or not.
Change your MQTTQosLevel to .atMostOnce
.
@yusufonderd is your issue resolved?
Unfortunately I can’t resolved. Issue still exists.