Function eventloop

Source
pub fn eventloop(
    options: MqttOptions,
    requests: impl Requests + 'static,
) -> MqttEventLoop
Expand description

Returns an object which encompasses state of the connection. Use this to create a Stream with stream() method and poll it with tokio.

The choice of separating MqttEventLoop and stream methods is to get access to the internal state and mqtt options after the work with the Stream is done or stopped. This is useful in scenarios like shutdown where the current state should be persisted or during reconnection when the state from last disconnection should be resumed. For a similar reason, requests are also initialized as part of this method to reuse same request stream while retrying after the previous Stream has stopped

let mut eventloop = eventloop(options, requests);
loop {
    let mut stream = eventloop.connect(reconnection_options).await.unwrap();
    while let Some(notification) = stream.next().await() {}
}

When mqtt stream ends due to critical errors (like auth failure), user has a choice to access and update options, state and requests. For example, state and requests can be used to save state to disk before shutdown. Options can be used to update gcp iotcore password