[Libwebsockets] libwebsockets_broadcast

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

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.
- In my callback function for this protocol, on LWS_CALLBACK_SERVER_WRITEABLE:
  - 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?
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.


Michel Archambault <marchamb at matrox.com> wrote: 

	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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20130327/f547755a/attachment-0001.html>

More information about the Libwebsockets mailing list