[Libwebsockets] Servicing multiple http requests

Andy Green andy at warmcat.com
Tue Aug 19 03:01:17 CEST 2014

On 29 April 2014 16:46:10 GMT+08:00, David Haykazyan <david.haykazyan at onetick.com> wrote:
>I am writing a http server using libwebsockets. It needs to be able to
>serve multiple requests simultaneously. When my callback is called with
>LWS_CALLBACK_HTTP reason, I read the input data, then launch a separate
>thread, that generates output data and sends it to the client using
>libwebsocket_write function. The callback returns 0, indicating that
>connection is not to be closed yet.

You need to take a different approach... the only threadsafe thing to call is the callback_on_writeable() api.  Then only in the WRITEABLE callback, from the service thread context, you should do any writing.

It can sync with data from another thread in the WRITEABLE callback to find what to send, but it all must be done from the one thread context that is used for lws service.

It is simply a singlethreaded library that is not threadsafe except the one exception about one_writeable() callback.


>My main loop looks like this:
>while (true)
>    libwebsocket_service(context, 10);
>    libwebsocket_callback_on_writable_all_protocol(&protocols[0]);
>This works fine as long as there is only one request at a time.
>However, if
>another client attempts a connection when there is already an active
>connection, my callback is never called with LWS_CALLBACK_HTTP reason
>the new connection. Is it a bug? Or maybe what I am trying to do is not
>supported at all? Thank you very much for your help.
>Yours faithfully,

More information about the Libwebsockets mailing list