[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [libmicrohttpd] websocket broadcast to all clients

From: David Gausmann
Subject: Re: [libmicrohttpd] websocket broadcast to all clients
Date: Wed, 20 Jul 2022 22:48:49 +0200
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0

Hi Bill,

maybe the libmicrohttpd example "src/examples/websocket_chatserver_example.c" could be interesting for you. Some time ago I wrote an extension for libmicrohttpd to support the Websocket protocol. This is the example file for it. But currently you only get the extension, if you compile --with-experimental.

In that example you have a chatserver, where multiple users can be connected to. If someone sends a chat message to the server, then the message will be broadcasted to each other user currently connected.

In this example I did this via two waiting threads per websocket. One thread is waiting for incoming messages from that websocket and the other thread is waiting for messages to send to that websocket. That sender thread of each websocket will be notified when a new message is received via a condition variable. The list of currently open websockets is memorized by the example application.

If you have questions about this example, feel free to ask me.

Kind Regards
David Gausmann

Am 20.07.2022 um 06:26 schrieb Christian Grothoff:
Dear Bill,

You should use a "MHD_NotifyConnectionCallback" to be notified about
when MHD is 'finished' with a connection.  Then, you can insert the
connection when the websocket mode is started into a data structure
(usually a doubly-linked-list (DLL) is better than an array) and remove
it during the above callback. To 'broadcast', you'd then iterate over
the DLL to send on each connection. Pseudocode:

struct ConnectionDllEntry {
   struct ConnectionDllEntry *next;
   struct ConnectionDllEntry *prev;
   struct MHD_Connection *conn;
   // other data structures you keep per connection

Then make sure to keep the above in the 'socket_context' you have per
MHD connection, and to clean up when the connection is finished:

my_notify_connection_cb (void *cls,
                          struct MHD_Connection *connection,
                         void **socket_context,
                          enum MHD_ConnectionNotificationCode toe)
   struct ConnectionDllEntry *cde = *socket_context;

   assert (connection == cde->connection);
   REMOVE_FROM_DLL (cde_head, cde_tail, cde);
   free (cde);

Anyway, I hope those bits give you enough of the idea.

Happy hacking!


On 7/19/22 23:47, Bill Bowling wrote:
I've been using libmicrohttpd for many years and have recently become
interested in using websockets.
I have a working version that seems to do what I want with a single client.
But, I want to broadcast a response to all connected clients.

I've searched through many posts and documents but have not stumbled
across how this could be done.

I think if I could place the connections in an array I could then loop
through them.
I have not been able to find enough information to pull this off.

Any help would be appreciated.

Bill Bowling

reply via email to

[Prev in Thread] Current Thread [Next in Thread]