Add server-side WebSocket compression#1114
Open
mkurz wants to merge 2 commits into
Open
Conversation
* Negotiate RFC 7692 permessage-deflate for server-side WebSockets. * Add compression settings, documentation, release notes, and MiMa filters. * Cover negotiation, compression, fragmentation, context takeover, max allocation, and low-level frame handling.
* Add WebSocketUpgrade overloads that accept a compressionEnabled flag. * Wire accepted WebSockets to decline negotiated compression per request. * Document and test per-upgrade compression disabling.
This was referenced Jun 30, 2026
pjfanning
reviewed
Jun 30, 2026
| @@ -0,0 +1,341 @@ | |||
| /* | |||
Member
There was a problem hiding this comment.
the source header should be the standard Apache header - see https://github.com/apache/pekko/blob/main/CONTRIBUTING.md#pull-request-requirements - unless this code is largely a copy/paste of 3rd party code (in which case, the existing source header in that code would be used)
pjfanning
reviewed
Jun 30, 2026
| * use the given handlerFlow to handle WebSocket messages from the client. The given subprotocol must be one | ||
| * of the ones offered by the client. | ||
| * | ||
| * The compressionEnabled flag allows declining negotiated WebSocket compression for this accepted WebSocket. |
Member
There was a problem hiding this comment.
can you add @since 2.0.0 on new public API methods?
pjfanning
reviewed
Jun 30, 2026
| * Dependency versions have been updated | ||
| * Jackson3 is supported in a new pekko-http-jackson3 lib | ||
| * Changed Java DSL methods that return Scala Durations to return Java Durations ([PR788](https://github.com/apache/pekko-http/pull/788)) | ||
| * Server-side WebSocket connections can now negotiate RFC 7692 `permessage-deflate` compression when requested by the client. |
Member
There was a problem hiding this comment.
2.0.0-M1 is already released. Just revert this and we will form 2.0.0-M2 release notes when that release is ready, hopefully in a month or 2.
Member
|
I see netty have many compression support, should we do that too? |
He-Pin
reviewed
Jul 1, 2026
| # Whether the server should support WebSocket compression using the RFC 7692 | ||
| # permessage-deflate extension. Compression is negotiated during the | ||
| # WebSocket handshake and is only used when the client requests it. | ||
| enabled = true |
Member
There was a problem hiding this comment.
Should we do adaptive compression?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
Adds server-side support for RFC 7692
permessage-deflateWebSocket compression.Compression is negotiated during the WebSocket handshake when the client offers
permessage-deflateinSec-WebSocket-Extensions. It is enabled by default and can be disabled globally through configuration.Also adds an API option to decline negotiated compression for a single accepted WebSocket upgrade.
Details
This PR adds:
permessage-deflatenegotiation for server-side WebSocketsclient_max_window_bitsWebSocketUpgradeoverloadsNetty comparison
The implementation follows Netty’s
permessage-deflatebehavior where it makes sense for Pekko HTTP:server_no_context_takeoverandclient_no_context_takeovercan be negotiatedclient_max_window_bitscan be negotiatedmax-allocationOne deliberate difference is zlib implementation scope. Netty also uses the JDK
DeflaterandInflaterAPIs for the default zlib settings, and only switches to JZlib when non-default zlibwindowBitsormemLevelsupport is needed. Pekko HTTP currently uses only the JDKDeflaterandInflaterAPIs, like Tomcat and Jetty. The JDK API does not expose zlibwindowBitsormemLevel, so Pekko HTTP does not acceptserver_max_window_bitsvalues below15and does not provide server window-size or memory-level settings.Clients may still request
client_max_window_bits; when they do, Pekko HTTP can include the configuredpreferred-client-window-sizein the handshake response to ask the client to use that window size for client-to-server messages.Security notes
WebSocket compression can increase CPU and memory usage.
The default context takeover settings match common server defaults and favor compression efficiency. Applications that need a more conservative setup can configure no-context-takeover behavior or disable compression globally/per accepted WebSocket.
max-allocationlimits decompressed message size. If the limit is exceeded while inflating a compressed message, Pekko HTTP closes the connection with a WebSocket protocol error.Testing
Ran:
sbt "http-core / Test / testOnly org.apache.pekko.http.impl.engine.ws.WebSocketServerSpec" sbt +mimaReportBinaryIssuesAlso verified Play Framework integration against the locally published Pekko HTTP artifact:
(This is a WIP pull request for Play which I will submit tomorrow - I will you ping you then)
References