[Libwebsockets] Help getting started with data from outside of the client callback loop

Nick Poole nickpoole at black-sphere.co.uk
Thu Aug 3 16:13:32 CEST 2017

Hi all,

I'm still just getting started with LWS, but I'm struggling to get my 
head around how the callback loop is intended to be used in the context 
of a WS client application that is producing data outside of the loop 
(I've already got a basic PoC working within the loop using static data).

I think what I'm looking for is a way of passing a reference to a 
message queue into the LWS connection context, so that within the loop, 
the callbacks can pop messages off the queue, serialise them and send to 
the WS server, and receive messages to be added to the queue (might need 
a send queue and a receive queue). I really want to avoid making these 
global variables.

The problem is that I can't see how to pass such references into the LWS 

* there's a per-connection user data object that's malloc'ed by LWS, but 
I don't seem to be able to access that from outside the callback. I'm 
also wondering how I should be free'ing any memory malloc'ed within the 
callback and referenced in the user data.

* there is a pointer to an object that can be set on the protocol 
struct, but I can't work out how you access that from inside the 
callback, and given that all the examples define the protocols as static 
const, I'm not sure whether dynamically building the struct during 
client setup is even the right thing to do.

Any hints or suggestions will be greatly appreciated.



More information about the Libwebsockets mailing list