Closed holotrack closed 9 months ago
I think this is the same usability issue as #23 in practice. I think explicitly requiring the user to run the ping is one of the things that saves resource usage on constrained devices (that doesn't need it), but the way it is now I'm not sure if it's achievable without refactoring the client.
@holotrack Here is an example of how to explicitly send a ping after some time:
use embassy_futures::select::{Either, select};
use embassy_time::{Timer, Duration};
loop {
match select(client.receive_message(), Timer::after(Duration::from_secs(2))).await {
Either::First(msg) => {
// Received message!
}
Either::Second(_timeout) => {
// Send ping
client.send_ping().await.unwrap();
}
}
}
@lulf Thank you very much. My problem is solved here is my working match/select function:
match select(
client.receive_message(),
Timer::after(Duration::from_secs(2)),
)
.await
{
Either::First(msg) => {
let (topic, message) = msg.unwrap();
info!("topic: {}, message: {}", topic, message);
}
Either::Second(_timeout) => {
info!("sending ping");
client.send_ping().await.unwrap();
}
}
I connecting such way to my MQTT server:
Connection is working and receiving messages but when there is no new message in MQTT subscirbed topic for around 30 sec im getting NetworkError. I tried to use such solution:
to sustain connection but i can not borrow client twice (i think this could be need to handle it this way: https://github.com/obabec/rust-mqtt/issues/23). Is it normal behavior of such dropping connection when there is no new messages? Shouldn't that send_ping() be implemented inside of that redeive_message functions?
Or im just using this library wrong way?