Skip to content

How can I send messages on a websocket whenever a tokio::sync::watch changes? #2652

Answered by fakeshadow
bergkvist asked this question in Q&A
Discussion options

You must be logged in to vote

actix does not suppor reference futures. which means anything like this would not work:

fn handle(&mut self, msg: M, ctx: &mut Context<Self>) {
    async {
        let _ = &self;
        let _ = &msg;
        let _ = &ctx;
    }
}

You sync channel usage is actually a stream and you can use Context::add_stream for handling it as ActorStream.

impl Actor for MyWebSocket {
    type Context = ws::WebsocketContext<Self>;
    fn started(&mut self, ctx: &mut Self::Context) {
        ctx.run_interval(HEARTBEAT_INTERVAL, |act, ctx| {
            if Instant::now().duration_since(act.heartbeat) > CLIENT_TIMEOUT {
                println!("Websocket Client heartbeat failed, disconnecting!");

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@bergkvist
Comment options

Answer selected by bergkvist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants