[Libwebsockets] ring API

Andy Green andy at warmcat.com
Mon Jun 17 20:18:46 CEST 2019

On 6/17/19 4:44 PM, Vitaly Shevtsov wrote:

Hi -

> Is it possible to keep the last (newest) message using ring API?

I am not 100% sure it's what you're describing, but lws_ring supports 
the idea that possibly many clients are using what was stored in the 
ring at different speeds, and the ring element is only finished with 
when all of the clients have consumed it.


Basically each client tracks his own tail in the ringbuffer, and only 
when the "oldest" tail has advanced beyond a ringbuffer entry may the 
entry be destroyed.  Fast clients consume them quickly and catch up with 
the head and fall silent, slower ones consume at their own pace, stuff 
waits in the ringbuffer until everyone has had it.

Extra handling is needed for dead clients


otherwise if one stops consuming, the ring can never destroy anything.


> I use ws-broker with one wsi connected to the server and many clients 
> connected to this broker. When new message comes from server, it must be 
> cached for future clients so that they can consume actual message 
> immediately without waiting new message from server.
> The only thing I came up with is to use message structure within vhd. 
> Something like this:
> struct per_vhd {
>      ...
>      struct lws_ring *ring;
>      struct msg * last; /* the last message we received */
>      ...
> };
> int callback_...(...) {
>     /* message from server, write it to cache for future clients and 
> send it to connected clients */
>     free(vhd->last);
>     vhd->last = create_msg(in,len);
>     lws_callback_on_writable_all_protocol_vhost(vhd->vhost, vhd->protocol);
>     /* client connected, send him a message from cache */
>     lws_callback_on_writable(wsi);
>     break;
>     /*
>     actually sending message from cache using lws_write(...) goes here
>     */
>     break;
>     return 0;
> }
> So my question is whether ring api allows so?
> Cheers.
> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> https://libwebsockets.org/mailman/listinfo/libwebsockets

More information about the Libwebsockets mailing list