uWebSockets.js icon indicating copy to clipboard operation
uWebSockets.js copied to clipboard

Update body parsing and file streaming exemples

Open RatchetS2 opened this issue 1 year ago • 5 comments

As a newcomer, these examples were difficult to understand and adapt to my needs. There were missing cork() calls and unoptimized code. So I share these complete examples that are simpler and more understandable.

RatchetS2 avatar Oct 27 '24 12:10 RatchetS2

Don't remove examples. Esp. not when the substitute is slower:

alexhultman@MacBookAir uSockets % ./http_load_test 256 localhost 9001 16 1
Using pipeline factor of 16
Using post with body
request size 1744
Running benchmark now...
Req/sec: 508488.000000
Req/sec: 510892.000000
Req/sec: 511276.000000
Req/sec: 513292.000000

alexhultman@MacBookAir uSockets % ./http_load_test 256 localhost 9001 16 1
Using pipeline factor of 16
Using post with body
request size 1744
Running benchmark now...
Req/sec: 482788.000000
Req/sec: 489336.000000
Req/sec: 489724.000000

It might look as "unoptimized code" to you, but it's not. Sure, there is a missing call to cork(), so you could add that call.

uNetworkingAB avatar Oct 27 '24 17:10 uNetworkingAB

I updated the body parsing example, you should get better performance. "unoptimized code" was maybe not the right word, I was talking about the size of the code. As these are examples, I think it’s important that they be easily understandable.

About changes: Rewriting "JsonPost.js" to "ParseJsonOrFormBody.js" and add URL-encoded form body parsing example. Merging and rewriting "VideoStreamer.js" and "VideoStreamerSync.js" to "FileStreaming.js".

RatchetS2 avatar Oct 27 '24 23:10 RatchetS2

I was talking about the size of the code. As these are examples, I think it’s important that they be easily understandable.

We don't optimize for size, we optimize for speed. And examples should show the best way for speed. If you want to optimize for size, use Express.

uNetworkingAB avatar Oct 28 '24 00:10 uNetworkingAB

We don't optimize for size, we optimize for speed. And examples should show the best way for speed. If you want to optimize for size, use Express.

I see, but it's not really newcomer friendly, the body parsing example was the one that makes me doubt to use uWS.js, it makes it look so hard just to parse the body. In any case, the updated examples should have the same performances while being simpler.

If performance is realy the main point and as parsing request body is an important feature, we could add a parseBody or onParsedBody function directly on the HttpResponse c++ wrapper. So Node.js would only have to process the final fulfilled ArrayBuffer. And we use the onData function for uploading or client side streaming.

RatchetS2 avatar Oct 28 '24 08:10 RatchetS2

I have updated the examples, again, they are now even simpler.

RatchetS2 avatar Jan 31 '25 00:01 RatchetS2