Websockets + broadcasting #2602
-
Hi there, thank you very much for this project. I am writing a small chat program in rust (i am quite new to rust ;). I use Rocket together with bootstrap5.3 for this) My Question would be: How would you implement broadcasting? Somehow we should have the necessary information from every client which is connected and then send any incomming message back to all clients... Thank you for any help ;) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Generally the way you handle this is to keep your own collection of connections. You can shove it into Rockets shared state so you can access it from your handlers. Then to broadcast, you simply iterate over the connections and send. This also allows you to selectively broadcast by filtering, etc. This is how it is done under the hood in every (?) WS library that offers a built-in broadcast from what I've seen. |
Beta Was this translation helpful? Give feedback.
Generally the way you handle this is to keep your own collection of connections. You can shove it into Rockets shared state so you can access it from your handlers.
Then to broadcast, you simply iterate over the connections and send. This also allows you to selectively broadcast by filtering, etc.
This is how it is done under the hood in every (?) WS library that offers a built-in broadcast from what I've seen.