[Libwebsockets] libwebsockets_broadcast

Michel Archambault marchamb at matrox.com
Wed Mar 27 14:57:36 CET 2013

Yes it's in another thread.
My thread that received data from hardware is created on an hardware event and ends (almost) after I call libwebsocket_callback_on_writable_all_protocol().

What you means by "it will blow up"?

So, it's better if I flag an event from my "hardware" thread to my websocket servicing thread to inform that data is available, so this thread should call libwebsocket_callback_on_writable_all_protocol() in my while loop?

Something like that :

   n = 0;
   while(n >= 0)
      if(event is flag)

      n = libwebsocket_service(context, 50);

Michel Archambault

-----Message d'origine-----
De : "Andy Green (林安廸)" [mailto:andy at warmcat.com] 
Envoyé : mercredi, mars 27, 2013 09:44
À : Michel Archambault
Cc : libwebsockets at ml.libwebsockets.org
Objet : Re: [Libwebsockets] libwebsockets_broadcast

On 27/03/13 21:39, the mail apparently from Michel Archambault included:
> I cannot do it in the service loop since the function that get data to
> broadcast is a callback call when data is available from an hardware event.
> What I have done is :
> - At init: pass a pointer (where I will copy my data) to
> libwebsocket_create_context() via lws_context_creation_info.user.
> - In my callback function on an hardware event:
>    - received the same pointer;
>    - copy the data at this memory location;
>    - call libwebsocket_callback_on_writable_all_protocol() for this
> protocol.

Yes this part is the right approach.

> - In my callback function for this protocol, on
> - retrieve my pointer using libwebsocket_context_user()
> - Write data to the weebsocket using libwebsocket_write()
> I would like to known if it is the good way to do it?

It is a good way but you don't explicitly point out if you're calling 
libwebsocket_callback_on_writable_all_protocol() from a different thread 
than the one you are servicing.

It it's all happening in one thread context, it's fine.

If libwebsocket_callback_on_writable_all_protocol() is coming from a 
different thread context than the service loop, eventually it will blow up.


> --
> Michel Archambault
> ------------------------------------------------------------------------
> *De :* Andy Green [mailto:andy at warmcat.com]
> *Envoyé :* mardi, mars 26, 2013 15:31
> *À :* Michel Archambault; libwebsockets at ml.libwebsockets.org
> *Objet :* Re: [Libwebsockets] libwebsockets_broadcast
> Hi -
> No the wsi holding thing isn't good, because the connection can have
> been closed, and the wsi freed any time. Afterwards the other thread can
> try to pass invalid wsi into the library.
> The safe way is perform wsi operations with a wsi that was handed to you
> in the callback, from within the callback. It's also safe to do
> operations on a protocol inside your service loop, ie call the
> callback-all-connections-of-this-protocol api from your service loop.
> You can synchronize calling it from your service loop and other thread
> by polling, if that's too much latency you can use a socket or file from
> the other thread using the external polling loop stuff to signal and
> pass data between the threads.
> It's better to eliminate your other thread at do it all in the service
> loop directly.
> -Andy
> Michel Archambault <marchamb at matrox.com> wrote:
>     Hi,
>     I have an application that generates data in one thread (name it the
>     "broadcast thread").
>     Previously, I used libwebsockets_broadcast() to sent this data to
>     all clients connected, but this function has been removed.
>     What is the good way to do that now?
>     Should I maintain a list of active clients (wsi), that my "broadcast
>     thread" will use to call libwebsocket_write() for each one of them?
>     Thank you!
>     --
>     Michel Archambault
>     ------------------------------------------------------------------------
>     Libwebsockets mailing list
>     Libwebsockets at ml.libwebsockets.org
>     http://ml.libwebsockets.org/mailman/listinfo/libwebsockets
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> http://ml.libwebsockets.org/mailman/listinfo/libwebsockets

More information about the Libwebsockets mailing list