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

MAHE Aubin aubin.mahe at fr.thalesgroup.com
Fri Oct 2 08:58:19 CEST 2015


Thank you all,

I agree with you, a message queue is mandatory.

The component I made is a gateway between a push only sub-system and web-browsers.
Sometimes when LWS_CALLBACK_SERVER_WRITEABLE event arrive I have no data to write and the data will be pushed to the gateway several milliseconds later.

Is LWS_CALLBACK_SERVER_WRITEABLE a periodic event? (which period?)

If not:
1. can I set a flag when LWS_CALLBACK_SERVER_WRITEABLE event arrive and use it later, when data will be pushed to the gateway?
2. how to reset the flag?

As you can see, the behavior of libwebsockets may be documented as a protocol, not only as an interface.
Where is the documentation of this protocol?

Thanks and Regards,
Aubin

-----Message d'origine-----
De : Harsha [mailto:andy.green at linaro.org] 
Envoyé : jeudi 1 octobre 2015 19:14
À : Fabian Kurz
Cc : libwebsockets at ml.libwebsockets.org
Objet : Re: [Libwebsockets] One thread for initializing and reading, another for writing

Yea I agree. I use it with a queue as well. I push my messages to a queue. And when you get a LWS_CALLBACK_SERVER_WRITEABLE I drain the queue. 

Harsha

> On Oct 1, 2015, at 12:53 AM, Fabian Kurz <andy.green at linaro.org> wrote:
> 
>> 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 
>> 'LWS_CALLBACK_CLIENT_WRITEABLE', 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.
> 
> BR
> Fabian
> 
> -- 
> Fabian Kurz, DJ1YFK      Munich, Germany
> fabian at fkurz.net         +49 176 24079617
> http://fkurz.net/        http://lcwo.net/
> _______________________________________________
> 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