[Libwebsockets] New user questions

Tony Mountifield tony at softins.co.uk
Mon Sep 5 16:12:20 CEST 2016

Hi, I've just joined this list because I'm looking for somewhere to ask
questions from the point of view of an experienced programmer who is
completely new to libwebsockets. Please let me know if there is a more
appropriate place.

I've just started to look at libwebsockets as a way to add websocket
handling to my multi-threaded application server as a replacement for the
current architecture, which is AJAX long polling over HTTP to a simple
http server embedded in the application.

I have two main questions:

1. I see that libwebsockets is structured as a single thread with an
event loop and callbacks. So when I receive a message from the client,
it causes a callback function to be called, such as

In the examples I have seen, callback_dumb_increment() does a very simple
string reversal and immediately returns the result as a message by calling

Am I correct in assuming that this callback executes within the main event
loop thread?

What happens if the message handling needs to do something that will take
some time, such as a database access, before it is able to send a
response? Does this cause ALL message handling for all connections to be
stalled until the callback returns?

I am used to having a separate execution thread for each open connection,
so that when one of them is waiting for I/O, it doesn't block the others,
and I'm having difficulty seeing how to achieve something similar with the
single-threaded libwebsockets architecture.

2. Other threads in my application generate events from time to time that
need to be delivered to interested client connections. In the AJAX long
polling architecture, these events are put into a per-client queue that
then gets emptied and sent by a "poll" request from the client.

It is one of the supposed advantages of a continuously-open websocket that
such events could be sent to the client immediately over the socket
without waiting for a poll. But what I have read about libwebsockets so
far suggests to me that only the main thread is allowed to do
libwebsocket_write() calls (although I may easily have misunderstood). So
do I need some kind of message concentrator to funnel the event messages

The above questions no doubt betray my lack of knowledge about how
properly to use libwebsockets, but if anyone could answer or point me to
any resources or examples that deal with such real-life scenarios and not
just "Hello world" examples, I would be very grateful indeed.


Tony Mountifield
Work: tony at softins.co.uk - http://www.softins.co.uk
Play: tony at mountifield.org - http://tony.mountifield.org

More information about the Libwebsockets mailing list