Architecture guidance for connected app

I'm porting a go application to rust, mostly to learn how to actually write an application in rust.

On the rust side there is a central processing loop that receives events through a central mpsc.

The application connects to a central server via a web socket. The server can send commands to the application via the ws, the application sends status to the server via the ws.

Handling the case where the ws is not present (but with periodic reconnections), vs. when the ws is present.

Should I even be splitting these streams?

It feels like I'd need code similar to below but it's unclear when/how the reconnection might occur.

loop {
    let ws_read: SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>;
    let ws_write: SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>;
    let mpsc_read: futures_channel::mpsc::UnboundedSender<MainEvents>;
    let mpsc_write::futures_channel::mpsc::UnboundedSender<MainEvents>;

    if ws_connected
           mpsc_read => { read local msg and write to ws_write, update the connection if the write fails? },
           ws_read => { read msg from server and write to local queue mpsc_write, update the connection if the read from ws fails? }
    } else {
       // to avoid the queue filling up?

       if should_reconnect_now:
           ws_read, ws_write = attempt_to_reconnect();

Are you looking for built-in methods for doing that?

I'm more looking to understand how one might handle the condition where in one case there might be a single thing to wait on, in another case two or three things to wait on. I've settled on an if with separate waits for my proof-of-concept application.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.