[Libwebsockets] How to send data asynchronously to the client?

Bas Appelo bas at appelo.nl
Fri Jun 20 14:29:48 CEST 2014

     libwebsocket_service(context, LARGE_NUMBER); //blocks

     libwebsocket_cancel_service(context); // wakes up thread-1

     libwebsocket_callback_on_writable(context, wsi);
     libwebsocket_service(context, LARGE_NUMBER); //blocks <= will call 
the callback!

Now you will receive a callback, it works like a charm.
So to write data you'll need to be in a callback, but the way above 
shows you how to get there/enforce one.

On 06/20/2014 02:18 PM, Mike M wrote:
> >>/  Hi,
> />>/
> />>/  1)Is there a way to asynchronously send the data to the client? That is,
> />>/  without being in a callback, server spontaneously initiating data
> />>/  transfer to the client..!!
> />
> >Libwebsockets is basically single-threaded.  But, with care from another
> >server thread, you can call either
> >
> >libwebsocket_callback_on_writable(), or
> >libwebsocket_callback_on_writable_all_protocol()
> >
> >but you need to manage your own list of live wsi / connections with the
> >first option (by tracking their creating and destruction in the
> >ESTABLISHED and CLOSED callbacks).
> >
> >You will get a LWS_CALLBACK_SERVER_WRITABLE as soon as it's possible to
> >write on that connection then, and you should figure out what to write
> >and write it from there.
> >
> >-Andy
> Hi,
> I am doing exactly this, but it doesn't work.
> I have a thread that calls libwebsocket_service with a huge timeout.  
> Writes, etc, are only ever done from within the callback.  When I need 
> to write something from another thread, I queue up the message that 
> needs to be written and then immediately call 
> libwebsocket_callback_on_writeable_all_protocol on the proto I'm 
> writing for.
> Unfortunately, I never get LWS_CALLBACK_SERVER_WRITEABLE until the 
> libwebsocket_service times out and gets called again.  This is totally 
> broken.  I saw a trac issue (#56) about using ppoll. Unfortunately, 
> the version of the library we're using doesn't seem to have that code 
> in it.  Any ideas?
> Thanks,
> -m
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> http://ml.libwebsockets.org/mailman/listinfo/libwebsockets

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20140620/26fc10ae/attachment-0001.html>

More information about the Libwebsockets mailing list