warmcat / libwebsockets

canonical libwebsockets.org networking library

Home Page:https://libwebsockets.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Decoupling the websocket protocol from the transport layer

sumitd2 opened this issue · comments

Hi team,
I would like to use libwebsockets with my own transport layer which is based on io_uring. Basically, I have a io_uring completion events based (single threaded) main loop (which runs a state machine) with non blocking socket calls. Is it possible with libwebsockets? Do you have any future plans to decouple the the websocket protocol from the transport layer?
Thank you.

We do have custom event lib support, where you can tell lws to handle socket events on an abstracted "event library", which can be you own implementation. I'm not sure if that's enough for this or not.

Eg,

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples-lowlevel/http-server/minimal-http-server-eventlib-custom/minimal-http-server.c

Thanks for the quick reply.

I am not sure if I understand the example, but one thing I certainly cannot do is to dedicate a thread to lws_service(). That way it will either occupy a core or incur a context switching overhead. My application is single threaded where it does all its work.

Ideally, what I want to be able to do is this:
When I receive data on my socket, I call LWS with that data. LWS then parses it and calls the registered callback with an appropriate event. This event could include data to be sent back through the socket. The data could be an http upgrade, or subsequent websocket frames. When my application wants to send data, it submits it to LWS which frames it and returns it to my application, which then sends it with one of io_uring's non-blocking send calls.

... nothing about this requires a dedicated thread. In fact it requires a single-threaded event loop.

Ideally, what I want to be able to do is this:

I'm afraid we're in the parallel universe where I wrote this 13 years ago without any reference to you, or your ideals you might arrive at in 2024. You're welcome to use or modify what's available, but nobody's going to rewrite it to your specifications for free.

I'm afraid we're in the parallel universe where I wrote this 13 years ago without any reference to you, or your ideals you might arrive at in 2024. You're welcome to use or modify what's available, but nobody's going to rewrite it to your specifications for free.

I certainly did not want you to rewrite it. I just thought you could let me know if there is something already there which could help me achieve similar to what I want. I am sure you would have come up with something better if you like the idea of decoupling the websocket protocol from the transport layer.
Thanks.

It's better to spend a bit more serious time understanding the custom event loop stuff (eg, that it is unrelated to any separate thread) that I did suggest. You're basically making an event library along the lines of libuv etc and that is how to interface to such a thing.

@lws-team
The custom event loop stuff is now making sense to me, but the example still relies on socket io being done by LWS. Is there an example, if decoupling is possible?