Skip to content

Sending to all connected websocket clients #2795

Closed Answered by scaprile
egnor asked this question in Ask questions, get answers
Discussion options

You must be logged in to vote

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

struct mg_str *data = (struct mg_str *) ev_data;
// Broadcast message to all connected websocket clients.
// Traverse over all connections
for (struct mg_connection *wc = c->mgr->conns; wc != NULL; wc = wc->next) {
// Send only to marked connections
if (wc->data[0] == 'W')
mg_ws_send(wc, data->buf, data->len, WEBSOCKET_OP_TEXT);
}

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by scaprile
Comment options

You must be logged in to vote
1 reply
@scaprile
Comment options

Comment options

You must be logged in to vote
1 reply
@scaprile
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants