babelouest / ulfius

Web Framework to build REST APIs, Webservices or any HTTP endpoint in C language. Can stream large amount of data, integrate JSON data with Jansson, and create websocket services
https://babelouest.github.io/ulfius
GNU Lesser General Public License v2.1
1.07k stars 183 forks source link

1:N or N:M websocket communication #158

Closed alexe100 closed 4 years ago

alexe100 commented 4 years ago

Hi! I am trying to plan a system that includes one or more web browsers, one websocket_server instance and one websocket_client. When websocket_client sends a message to the websocket_server, I would like to echo that message to all browser connection "instances". In this case, in the websocket_server websocket_incoming_message_callback I need to get message (last_message->data) from websocket_client and, by hand, write that message to all browsers endpoint connections, right?

If we have just one websocket endpoint on the websocket_server, where can I get the the list of browsers websocket connections endpoints in order to echo messages to all of them? Is there any sample that does this?

Thank you Alex

babelouest commented 4 years ago

That's what the *_user_data are for, they are app driven pointers that you can use to pass any data you want. Typically in your case you can use those pointers to pass a database handler, a shared structure or a structure. You can check the websocket management in Taliesin to get an idea: https://github.com/babelouest/taliesin/blob/master/src/webservice.c Since all websocket instances are in a separated thread, you need to be careful with the concurrent memory access, that's why I use mutexes and cond.

alexe100 commented 4 years ago

Great! Thank you