[Libwebsockets] Performance Challenges vs socket.io

Andy Green andy at warmcat.com
Tue Jun 7 04:14:39 CEST 2016

On 06/07/2016 09:35 AM, Ignas wrote:
> Hi Guys,
> I was really happy being able to compile and get libwebsockets working.
> However I am having some challenges with understanding performance of
> libwebsockets on windows.I did a bit of a read on the google group, but
> seems people are taking about unrelated issues.
> In terms of performance. I did a very simple test that goes loads the
> html/javascript for client, waits 5 seconds then and pings the server
> over websocket and then downloads 30k worth of data from a server over
> the websocket. Unfortunately what I am trying to do is latency critical
> and I compared to socket.io <http://socket.io> I can see at least 50+ ms
> delay. Using wireshark I have narrowed it down to basically libwebsocket


> not pushing the frames as fast. Could anyone take a look at my test code

Unfortunately aside from the other considerations lws is a library, but 
what gets tested is library + some other code.

I appreciate you sent your code, anyway.

> and see if there is anything I can do to improve it (besides making it
> more readable of course)?
> http://pastebin.com/91Z6zUQj
> Considering it is just the "lws_service(context, 0);" line I am not sure
> if there is.
> I am attaching wireshark captures in case you would like to compare those:
> https://www.dropbox.com/sh/ik8txohp9m76948/AADXSJUxvgy6zIwEcmi_3zK1a?dl=0
> From what I can see after the 5 second mark once the protocol has
> initialised, the diffence is that libwesockets are using RSL and IPA
> protocols compared to socket.io <http://socket.io> TCP.
> Regards,
> Ignas
> Cell +44 7938015569
> ---
> "A week of coding can spare an hour of thought."

This is completely wrong -->

/* *
* websocket_write_back: write the string data to the destination wsi.
int websocket_write_back(struct lws *wsi_in, char *str, int str_size_in)
     int n;
     int len;
     unsigned char *out = NULL;

     char *write_what = reply_string;

     out = (unsigned char 
*)malloc(sizeof(char)*(LWS_SEND_BUFFER_PRE_PADDING + len + 
     //* setup the buffer*/
     memcpy(out + LWS_SEND_BUFFER_PRE_PADDING, write_what, len);
     //* write out*/
     n = lws_write(wsi_in, out + LWS_SEND_BUFFER_PRE_PADDING, len, 

     //* free the buffer*/

     return n;

1) len is uninitialized!  Your compiler should have complained.  You are 
malloc()-ing up some random amount of memory.

2) the code is terminally confused (str is not used, global reply_string is)

3) You do not need to malloc up a buffer there at all.

If lws finds your socket can accept what you want to send, it sends it 
right away.

If it could only send part of it, it malloc()'s up its own buffer and 
sends the rest later.


         printf(KCYN_L"[Main Service] Server recvived:\n"RESET/*, (char 

         //* echo back to client*/
         websocket_write_back(wsi, (char *)in, -1);


You can't write from the receive callback.  You have to ask for 
lws_callback_on_writable() and write it in the 

Actually if you don't take care about that, in another implementation 
say, it will use an unbounded amount of memory holding your async write 
data in the case many receives are coming but that connection cannot 
accept writes.


> _______________________________________________
> Libwebsockets mailing list
> Libwebsockets at ml.libwebsockets.org
> http://libwebsockets.org/mailman/listinfo/libwebsockets

More information about the Libwebsockets mailing list