CocoaMQTT icon indicating copy to clipboard operation
CocoaMQTT copied to clipboard

How can create multiple instance for CocoaMQTT

Open sw-tt-rushabhpatel opened this issue 4 years ago • 1 comments

How can we connect multiple devices using mqtt ? ``` mqtt = CocoaMQTT(clientID: mqttConfig.shared.clientID, host: mqttConfig.shared.host, port: UInt16(mqttConfig.shared.port)) mqtt!.username = mqttConfig.shared.username mqtt!.password = mqttConfig.shared.password mqtt!.keepAlive = 60 mqtt!.delegate = self mqtt!.enableSSL = true mqtt!.cleanSession = true mqtt!.connect()

sw-tt-rushabhpatel avatar Sep 07 '20 11:09 sw-tt-rushabhpatel

Creates an empty array of CocoaMQTT called instanceArray. You can add elements to this array and create multiple instance.

        let hostArray:[String] =  ["host1", "host2", "host3"]
        var instanceArray = [CocoaMQTT]()
        for host in hostArray {
            mqtt = CocoaMQTT(clientID: host + String(ProcessInfo().processIdentifier), host: host, port: 1883)
            //...
            instanceArray.append(mqtt!)
           
        }
        
        print("clientID = \(instanceArray[2].clientID)")

leeway1208 avatar Sep 23 '21 07:09 leeway1208