Is it possible to send shared Arc<Message>
via the websocket connection?
#1976
-
Sorry for bothering again In my application, I have a lot of broadcasted messages, and a lot of cloning done because of that. I've tried to choose Is there a way not to clone messages that would be broadcast to several WebSocket what I'm doing is something like this: struct Handler {
/// Split socket into sender and receiver
async fn handle_socket(&self, socket: WebSocket) {
let (sender, _) = socket.split::<Arc<Message>>();
/* ... */
self.senders.insert(key, sender);
}
/// Try to send the same messages to lots of senders
async fn broadcast(&self, messages: Vec<String>) {
let messages = messages
.into_iter()
.map(|Message::Text)
.map(Arc::new)
.collect::<Vec<_>>();
for sender in self.senders.values_mut() {
let mut stream = tokio_stream::iter(messages.clone()).map(anyhow::Ok);
sender
.send_all(&mut stream)
.await
.unwrap();
}
}
} This probably depends on what tokio-tungstenite is capable of. If so, I will file an issue there also. Edit: or maybe I need to find another SplitSink implementation 🤔
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Nope you need to clone the messages. |
Beta Was this translation helpful? Give feedback.
Nope you need to clone the messages.