[Libwebsockets] One thread for initializing and reading, another for writing

Fabian Kurz fabian at fkurz.net
Thu Oct 1 09:53:58 CEST 2015

On Thu, Oct 01, 2015 at 03:37:11PM +0800, Andrew Cooks wrote:
> On Wed, Sep 30, 2015 at 8:28 PM, MAHE Aubin wrote:
> > I use a thread for reading on libwebsockets and another one for writing.
> > [...]
> > Is this practice safe?
> >
> In README.coding.md there is a section that says that you should only
> 'send' when you receive the 'LWS_CALLBACK_SERVER_WRITEABLE' or
> which I interpret to mean that it is not safe to call libwebsocket_write()
> from another thread.
> I'm new to libwebsockets too (so my solution might not be optimal), but for
> my application I implemented a message queue that I could write to from
> another thread and read from inside the 'LWS_CALLBACK_CLIENT_WRITEABLE'
> callback. It took a bit of work to get the publish/subscribe mechanics to
> work properly to support multiple clients.
> I'd like to learn how others have approached this problem too.

I something similar, using Redis for the message queues. Works very
well (and makes it trivially easy to use/process/change the data from
other processes as well) and requires very little programming effort;
just include hiredis and run a local redis instance.


Fabian Kurz, DJ1YFK      Munich, Germany
fabian at fkurz.net         +49 176 24079617
http://fkurz.net/        http://lcwo.net/

More information about the Libwebsockets mailing list