Asynchronous webserver with websockets

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Asynchronous webserver with websockets

Postby PeterR » Mon Jul 01, 2019 11:31 am

I need a simple asynchronous webserver with websockets.
I could accept asynchronous FTM and convert/add websockets latter.

I have had a quick look at:
1) HTTP Server.
Does not seem to be asynchronous and lacks websockets.
Its not clear if the HTTP server interface supports multiple threads (start/stop aside).
For example I could add worker tasks and have the event handlers push the request object into a queue.
The task may then httpd_resp_send_chunk().
Is this mode supported?

2) ESPAsyncWebserver
ESP-IDF v4.0-dev-562-g2b301f53e will not compile with ardunio 1.0.3-rc1-9-g7d78247
I am aware of https://github.com/espressif/arduino-esp32/issues/1142 but need features of 4.0. Also I am not sure how nice Ardunio will play with the IDF components I need.

3) Kolban's HTTPServer
Not asynchronous & also (unless I am confussed) seems to close the websocket after each request?

Any help/suggestion welcome.
& I also believe that IDF CAN should be fixed.

permal
Posts: 384
Joined: Sun May 14, 2017 5:36 pm

Re: Asynchronous webserver with websockets

Postby permal » Mon Jul 01, 2019 12:31 pm

May I ask why you need it to be asynchronous? Also, in what part of the server do you need it? Do you just need multiple simultaneous connections?

Keep in mind that the wifi & lwip tasks takes up quite a bit of resources, leaving you less than two cores for your own work. If you're thinking you want a a task/thread per client, I suggest you think again. That's how it was done in the old days, but it doesn't scale well.

In the the HTTP(s) server I'm implementing I'm using a single thread to handle all HTTP requests while the actual socket send/receive operations are handled by a separate one (what I call the SocketDispatcher) via event messaging. As such, the HTTP(s) server can process a request while data is being transmitted to/from a client.

If that is asynchronous enough for you, and the GPL-3.0 license is compatible with your use case, have a look at my http_server_test. It's all work in progress (I build what I need for my own use case) and it relies on the Smooth framework. I haven't implemented WebSockets yet and I'm sure there are things that can be improved. Pull Requests are welcome.

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: Asynchronous webserver with websockets

Postby PeterR » Mon Jul 01, 2019 1:32 pm

Thanks.
May I ask why you need it to be asynchronous?
I want the HTTP engine to handle multiple connections and not to block because of traffic on a connection. There can be a limit to the number of workers of course.

Lets take Kolban's webserver as an example; a connection socket is create with accept(), the request details are obtained from that client socket before being passed to the response handler and processed.
If we are serving a large file then all other HTTP transactions would be stalled until the current request has been completed and/or timed out.
Small and regular AJAX and/or websocket exchanges would have to wait.
I want a scheme where the small data transactions may be given priority.
As I said it would be easy enough to send the request to one of more handler tasks. What I do not know is if the supplied HTTP server code may be used from multiple threads.

Your approach is close to what I want. I would want multiple SocketDispatcher's however and I cannot use GPL.
& I also believe that IDF CAN should be fixed.

PaulFreund
Posts: 45
Joined: Wed Nov 15, 2017 9:07 pm

Re: Asynchronous webserver with websockets

Postby PaulFreund » Tue Jul 02, 2019 6:34 pm

I would suggest using https://github.com/chmorgan/libesphttpd which is a continuation of Spritetm's libesphttpd. By default it is processing in a FreeRTOS task and can handle multiple connections simultaneously. It is async to the degree that in every cycle polling for data all client sockets are processed one after another. Downloading big files is done by calling the endpoint handler multiple times with additional data so it should not block small requests. I think for most applications this is async enough, only multiple tasks preemting each other could create smaller processing intervals but I doubt it is neccessary.

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: Asynchronous webserver with websockets

Postby PeterR » Wed Jul 03, 2019 10:17 am

Great. That's async enough for me! End user experience 'real time' not hard!
& I also believe that IDF CAN should be fixed.

Who is online

Users browsing this forum: Bing [Bot] and 98 guests