Comments (7)
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.
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.
@ksinghal did this work?
from mqtt-nio.
Closing as received no response
from mqtt-nio.
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.
@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.
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)
- MQTTClient do not ping HOT 3
- Xcode preview fails to build with errors HOT 8
- `MQTTClient.logger` should be a `let` not `var`
- MQTTNIO has moved
- Change MQTTError into a struct
- Implement AUTH workflow for connect
- Remove properties parameter from MQTTClient.publish
- Add async ping function HOT 1
- Remove deprecated functions for next major version
- Watch OS 9 Beta: Couldn't establish connection to MQTT server, getting connection timeout error HOT 6
- Publication preconditions HOT 4
- MQTTProperties.subscriptionIdentifier accepts UInt, but gets serialised as Int HOT 1
- Async form of MQTTClient.publish does not respect `retain` parameter HOT 2
- Doubt: Timeouts when connecting, subscribing, and unsubscribing HOT 2
- V2.9.0 causes CLIENT_ERROR disconnect from AWS shadow, but v2.7.0 does not HOT 6
- Not all network interruptions are resulting in prompt close listener calls or isActive state update HOT 1
- Add Tasks by writing to channel HOT 1
- Build errors with Xcode 13.2 HOT 5
- Tidy up TSTLSConfiguration.init functions
- Keep alive messages not sent HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mqtt-nio.