Skip to content

Exposing data coming from a tokio::sync channel to a websocket. #2758

Answered by Krahos
Krahos asked this question in Q&A
Discussion options

You must be logged in to vote

Solved: I had to implement a different StreamHandler for the type I'm generating in the server. Full example code for anyone else stumbling on this:

use std::{net::TcpListener, thread, time::Duration};

use actix::{Actor, AsyncContext, SpawnHandle, StreamHandler};
use actix_web::{
    web::{self, Data, Payload},
    App, Error, HttpRequest, HttpResponse, HttpServer,
};
use actix_web_actors::ws::{self};

use tokio::sync::watch::{channel, Receiver};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    // Define communication channel.
    let (sender, receiver) = channel(Message { int: 0 });

    // Spawn sender thread.
    let _handle = thread::spawn(move || loop {
        thread::

Replies: 1 comment 1 reply

Comment options

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

Answer selected by Krahos
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