snapview / tokio-tungstenite

Future-based Tungstenite for Tokio. Lightweight stream-based WebSocket implementation
MIT License
1.88k stars 236 forks source link

Is message cloning necessary when broadcasting? #271

Closed rezu-jtr closed 1 year ago

rezu-jtr commented 1 year ago

In a simple scenario, let's say a chat, when I have to broadcast all incoming websocket messages to all server participants, do I have to clone it as many times as the number of participants? The message is a text and is constant. Shouldn't I be allowed to send Arc<Message> to websocket streams/sinks?

daniel-abramov commented 1 year ago

We have an old issue to address: https://github.com/snapview/tungstenite-rs/issues/96#issuecomment-1031524909 I think we even agreed upon the solution (see discussion and linked issues), but it has not been fully implemented.

However, it does not mean that we won't perform any copies at all, since we would need to internally allocate a buffer when sending a message, so the message will still be copied (sort of), yet it would indeed be more efficient than copying the Message just before passing to the sink (given that the message will be copied and dropped after that). So yeah, for a broadcast scenario of a large message to many participants, we might be a bit inefficient.

I'm going to close this one since we have an open tracking issue for this. Once it's implemented in tungstenite, adapting tokio-tungstenite won't be a big deal.