MQTT-Client-Framework icon indicating copy to clipboard operation
MQTT-Client-Framework copied to clipboard

Cannot subscribe topic

Open yusufonderd opened this issue 6 years ago • 9 comments

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>

yusufonderd avatar May 15 '18 07:05 yusufonderd

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?

ckrey avatar May 15 '18 09:05 ckrey

@yusufonderd What is your subscriptionTopic here? Does it include device-id?

mendirattanishant avatar May 15 '18 16:05 mendirattanishant

@mendirattanishant My subscriptionTopic is custom string. It starts with 'indoor/position/..'

yusufonderd avatar May 15 '18 17:05 yusufonderd

@yusufonderd and it works fine in Android client? In my case it was not connecting as I was not setting the clientId correctly.

mendirattanishant avatar May 15 '18 17:05 mendirattanishant

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 avatar May 16 '18 06:05 yusufonderd

@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.

mendirattanishant avatar May 16 '18 17:05 mendirattanishant

Change your MQTTQosLevel to .atMostOnce.

Adrxx avatar Jul 07 '18 07:07 Adrxx

@yusufonderd is your issue resolved?

jcavar avatar Aug 14 '18 14:08 jcavar

Unfortunately I can’t resolved. Issue still exists.

yusufonderd avatar Aug 14 '18 15:08 yusufonderd