r/learnrust•
PonderingClam

How do I share data between coroutines in an axum websocket server?

I'm doing a small project where I'm building a websocket server in rust - which is meant to manage some shared state between multiple clients.

I want one worker coroutine per client - which will constantly listen for commands from the client - and upon executing a command (which might read / write from a file on disk), this coroutine should then be able to broadcast an event to all other clients.

My problem, is that I cannot figure out how to share A: the list of all clients, and B: access to the database.

Here is some of the code I am using now:

#[derive(Clone)]
pub struct BattleMapServer {
	clients: Arc<RwLock<Vec<WebSocket>>>,
	database: Arc<BattleMapDatabase>,
}

impl BattleMapServer {
	pub fn new() -> Self {
		Self {
			clients: Arc::new(RwLock::new(Vec::new())),
			database: Arc::new(BattleMapDatabase::new("battlemap.db")),
		}
	}

	#[debug_handler]
	async fn websocket_handler(ws: WebSocketUpgrade, 
		State(server): State<BattleMapServer>) -> Response {
		return ws.on_upgrade(async |socket| { let _ = 1; });
	}

	pub async fn run(self) {
		let app = Router::new()
			.route("/ws", get(BattleMapServer::websocket_handler))
			.with_state(self);
		let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap();
		axum::serve(listener, app).await.unwrap();
	}
}

The compiler is giving me the following bits of information:

future cannot be sent between threads safely
the trait `Sync` is not implemented for `(dyn hyper::upgrade::Io + std::marker::Send + 'static)`rustcClick for full compiler diagnostic
server.rs(25, 51): captured value is not `Send`

I'm guessing the problem is my BattleMapServer struct does not implement Sync or Send - but I don't really know what that means or how to fix it.

I would appreciate any advice here. What does this error mean and how would you fix it? Or better yet, is what I'm doing here not very idiomatic rust? Is there a better way to go about this?

Posted Oct 2, 2025, 1:47 PM UTC

15
Loading comments…