CocoaMQTT icon indicating copy to clipboard operation
CocoaMQTT copied to clipboard

Mqtt not connecting immediately?

Open vadlani9 opened this issue 1 year ago • 3 comments

Mqtt is not connecting immediately, it's taking upto 6-7 sec's of time to connect in iOS swift. Is it common in iOS or is there any issue in my development.

`func connectMqtt(mqttProfile: MQTTConfiguration, handler: @escaping (Result<String,MQTTError>)->()){
let clientID =  String(ProcessInfo().processIdentifier)
mqttReference = CocoaMQTT(clientID: clientID, host: mqttProfile.baseURL, port: UInt16(mqttProfile.port))
mqttReference?.username = mqttProfile.userName
mqttReference?.password = mqttProfile.password        

DispatchQueue.global(qos: .userInitiated).async { [self] in
    //mqttReference?.logLevel = .debug
    mqttReference?.enableSSL = true
    mqttReference?.sslSettings = [
        GCDAsyncSocketManuallyEvaluateTrust: NSNumber(booleanLiteral: true),
        GCDAsyncSocketUseCFStreamForTLS: NSNumber(booleanLiteral: false),
        kCFStreamSSLPeerName as String: "" as NSString,
        GCDAsyncSocketSSLProtocolVersionMin: NSNumber(integerLiteral: Int(SSLProtocol.tlsProtocol1.rawValue)),
        GCDAsyncSocketSSLProtocolVersionMax: NSNumber(integerLiteral: Int(SSLProtocol.tlsProtocol12.rawValue)),
    ]
    
    ///mqttReference?.keepAlive = 300
    ///mqttReference?.cleanSession = false
    mqttReference?.autoReconnect = true
    mqttReference?.backgroundOnSocket = mqttProfile.allowBackgroundMQTT
    mqttReference?.delegate = self
    do {
        if((mqttReference?.connect())!){
            mqttReference?.didConnectAck = { mqtt, ack in
                switch ack {
                case .accept:
                    handler(.success("Connected"))
                    self.isMqttConncted = true
                    privateVariables.shared.mqttEnabled = true
                    BlazeClient.shared.mqttConnected = true
                    self.didReciveMqttMessage(.init(topic: "connectivity", string: "connected"), 1)
                default:
                    handler(.failure(.badURL))
                    self.isMqttConncted = false
                    privateVariables.shared.mqttEnabled = false
                    self.didReciveMqttMessage(.init(topic: "connectivity", string: "disconnected"), 0)
                }
            }
        }
    }
}
}`

Delegate:

`internal func mqtt(_ mqtt: CocoaMQTT, didConnectAck ack: CocoaMQTTConnAck) {
switch ack {
case .accept:
    SDKdebug().debug(value:"mqtt connected")
    self.isMqttConncted = true
    privateVariables.shared.mqttEnabled = true
    BlazeClient.shared.mqttConnected = true
    self.didReciveMqttMessage(.init(topic: "connectivity", string: "connected"), 1)
default:
    
    SDKdebug().debug(value:ack.description)
    
    self.isMqttConncted = false
    privateVariables.shared.mqttEnabled = false
    BlazeClient.shared.mqttConnected = false
    
    DispatchQueue.main.async {
        self.startToMQTT { (sttaus) in
            
        }
    }
    
}
}`

vadlani9 avatar Apr 18 '23 11:04 vadlani9

Hi. It is also related to the responding speed of the broker server.

leeway1208 avatar Apr 19 '23 12:04 leeway1208

Which is working fine in android, this delay happening only in iOS.

vadlani9 avatar Apr 20 '23 11:04 vadlani9

I think you can write some time output in the code to detect where it takes the longest time.😄

leeway1208 avatar Apr 21 '23 14:04 leeway1208