Writing to multiple `Sink`s concurrently

I'm using tokio-tungstenite for WebSocket connections, and I have a dispatch_global(event) function that should dispatch an event to every connection, the blocking method for this is

for mut conn in self.conns.iter_mut() {
    conn.send(message.clone()).await?;
}

self.conns is a DashMap and conn is WebSocketStream which implements Sink, send is SinkExt's method from futures-rs, so I'm stuck here because whatever I do will give me lifetime errors

I suspect it would be helpful to post those errors.

My general recommendation is that IO resources do not go in hash maps, and that you should have only one IO resource per task. Consider using the actor pattern for controlling your IO resource.

I ended up spawning a different thread for each WebSocket connection and using a channel to manage them, is this what you meant?

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.