Giter Club home page Giter Club logo

Comments (7)

ksinghal avatar ksinghal commented on June 10, 2024

Also wanted to add snippets for closeClient() and createMQTTClient(jwtString:):

    private func createMQTTClient(jwtString: String) throws -> MQTTClient {
        try stopClient()

        return MQTTClient(
            host: Constants.HostURL,
            port: Constants.Port,
            identifier: clientIdentifier,
            eventLoopGroupProvider: .createNew,
            configuration: .init(userName: Constants.UserName,
                                 password: jwtString,
                                 useSSL: true,
                                 tlsConfiguration: .niossl(tlsConfiguration))
        )
    }


    public func stopClient() throws {
        try client?.disconnect().wait()
        try client?.syncShutdownGracefully()
    }

from mqtt-nio.

adam-fowler avatar adam-fowler commented on June 10, 2024

You shouldn't need to shutdown the client if you lose the connection. You should be able to call

client.connect(cleanSession: false)

to reconnect. Obviously if you have lost connection because of a lose of network it isn't guaranteed you will reconnect until the network comes back.

from mqtt-nio.

adam-fowler avatar adam-fowler commented on June 10, 2024

@ksinghal did this work?

from mqtt-nio.

adam-fowler avatar adam-fowler commented on June 10, 2024

Closing as received no response

from mqtt-nio.

flowbe avatar flowbe commented on June 10, 2024

Hi, I'm trying to achieve an auto-reconnect feature but I'm not sure how to proceed. Right now my code is simply:

client.addCloseListener(named: "Close listener") { _ in
    DDLogWarn("Connection closed, try to reconnect")
    _ = client.connect(cleanSession: false)
}

But I'm not sure if it's the right way to proceed. Perhaps, a built-in auto-reconnect would be nice

from mqtt-nio.

adam-fowler avatar adam-fowler commented on June 10, 2024

@flowbe In the iOS sample I wrote, I have the following. I moved the call to the main thread because I also had some UI changes to make but I guess your code above would work as well. The only thing you'll need to watch out for is you have a small amount of time where you aren't connected and any attempts to send messages over that time will fail.

self.client.client?.addCloseListener(named: "EmCuTeeTee") { result in
    DispatchQueue.main.async {
        self.connect(cleanSession: false)
    }
}

from mqtt-nio.

flowbe avatar flowbe commented on June 10, 2024

Thanks for your reply. I tried this code but I noticed that it doesn't do what I want because if the reconnect fails then it's over. Here is a sample code for a smarter reconnect method:

let reconnectDelay: Double = 5
let queue = DispatchQueue(label: "mqttqueue")

client.addCloseListener(named: "Close listener") { _ in
    print("Connection closed")
    queue.async {
        reconnect()
    }
}

func reconnect() {
    guard !client.isActive() else { return }
    print("Reconnecting…")
    do {
        _ = try client.connect(cleanSession: false).wait()
        print("Connection successful")
    } catch {
         print("Error while connecting: \(error)")
         queue.asyncAfter(deadline: .now() + reconnectDelay) {
            self.reconnect()
        }
    }
}

from mqtt-nio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.