Sending to all connected websocket clients #2795
-
|
I'd like to be able to broadcast a message to all websocket clients currently connected. I'm thinking this would be something like this is that... reasonable? I feel like I'm mucking around in undocumented innards here. Any gotchas? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 4 replies
-
|
Please see our documentation, and follow the guidelines in our tutorials. mongoose/tutorials/core/multi-threaded-12m/main.c Lines 69 to 76 in 793171e |
Beta Was this translation helpful? Give feedback.
-
|
Ah I missed that thank you!! Maybe link that in the "WebSockets" section of the tutorials list, I didn't think to look in the "Timers" section! |
Beta Was this translation helpful? Give feedback.
-
|
Yeah I was just thinking it could be linked, it's clearly a timer tutorial mainly. For websockets specifically "send to all" is a common thing one might look for within the websocket interface so a breadcrumb "also see this one" link would be handy. (Your system has the ability to "send to all that you've tagged a certain way" which is even better.) Just a mild suggestion, thanks for the excellent code-- probably the most well organized straight-C protocol stack implementation I've ever seen, I have no idea how you managed to make it so elegant when most are so encrusted with pseudoabstractions. Kudos! |
Beta Was this translation helpful? Give feedback.
-
|
Hello, Sergio. Having similar needs and read the tutorials, but.. My situation is like : Open Browser#1, establish ws logger. Data pushed to B#1. Then open B#2, establish same ws, data falling then to B#2, not to B#1 anymore - although it is still open, no connecten reset occurs, keep-alive messages (Wireshark) are coming from B#1, but no ws data(!). Then final step - closing B#2, and ws logs are neatly return to B#1. Without re-triggering ws connection at all. The only difference from https://mongoose.ws/documentation/tutorials/core/timers/#websocket-broadcast is using mg_ws_printf() instead of mg_ws_send(). |
Beta Was this translation helpful? Give feedback.
-
|
Can you post a complete repro case @Juggler-5 ? |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.

Please see our documentation, and follow the guidelines in our tutorials.
There are several tutorials doing that.
https://mongoose.ws/documentation/tutorials/core/timers/#websocket-broadcast
mongoose/tutorials/core/multi-threaded-12m/main.c
Lines 69 to 76 in 793171e