Actix websocket run_interval

Please, is there a way to exit a RUN_INTERVAL without stopping the context ??

Actix uses Tokio internally, so you could spawn an ordinary Tokio task that performs the operation:

use tokio::time::{self, Duration};

let handle = tokio::spawn(async {
    let mut interval = time::interval(Duration::from_millis(10));

    loop {
        interval.tick().await;
        do_something().await;
    }
});

Then, you can call .abort() on the handle to kill the task.

here is my code, i dont see how to use a tokio task here? these are websocket messages that I have to send every time interval, everything works fine unless I have to exit the RUN_INTERVAL loop without stopping the ctx context!

impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWebSocket {

fn handle( &mut self, msg: Result<ws::Message, ws::ProtocolError>,ctx: &mut Self::Context,) {
    match msg {           
        Ok(ws::Message::Text(msg)) => {
                if &msg == "valmsg"{
               ctx.run_interval(Duration::from_secs(2), |act, ctx|{ 
                    getdb::donnees6().into_actor( act).map(|res, act, ctx|{                
                        let bro: String = res.unwrap_or_else(|e| format!("{}", e)); 
                        ctx.text(bro);   
                        }).spawn(ctx);                           
                    });
               }
         }
}

}

I don't know how to make it talk to actix in that way, sorry. You would need to completely go around actix to use my approach.

1 Like

thank you Alice, when I find a solution I will let you know :slightly_smiling_face:

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.