NanoHttpd / nanohttpd

Tiny, easily embeddable HTTP server in Java.
http://nanohttpd.org
BSD 3-Clause "New" or "Revised" License
6.89k stars 1.69k forks source link

permessage-deflate for WebSockets #615

Open stefan-reich opened 2 years ago

stefan-reich commented 2 years ago

WebSocket compression (permessage-deflate) is now well-supported in browsers.

Has anyone attempted to add it to NanoHTTPD?

(For context: For very interactive pages I have moved to a mode where the whole page is delivered through a websocket as this simplifies dynamic page updates a lot, so at that moment I have lost the automatic GZIP compression for HTML content. Example page with a lot of WebSocket activity served directly from NanoHTTPD.)

NoahAndrews commented 2 years ago

I would strongly consider using Java-WebSocket over NanoHTTPD. It has compression support IIRC, and has generally seemed significantly more reliable to me. I had a lot of weird issues with WebSockets in NanoHTTPD when I tried it.

You'll have to have WebSocket traffic be on a different port, but that's not the end of the world.

stefan-reich commented 2 years ago

Yes there were issues at some point with NanoHTTPD's websockets. Not sure which version it was or when, but NanoHTTPD added spurious null bytes at the end of WebSocket messages. Since I fixed that, everything is running smoothly.

If I switch to a completely different library, I will have to change a lot of infrastructure. As it is now, I can parse request parameters, headers etc. exactly the same way between a HTTP request and a WebSocket. And NGL, having the same port for http and websockets is pretty nice too.

I may actually add the compression myself, I think it's doable (although the RFC makes it a little more complicated than one might expect). Websites completely built on NanoHTTPD are my ticket to fame, lol.