-
Notifications
You must be signed in to change notification settings - Fork 23
permessage-deflate compression for websockets #337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🦋 Changeset detectedLatest commit: 03ac79d The changes in this PR will be included in the next version bump. This PR includes changesets to release 14 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
c10d811
to
0cde51b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR enables permessage-deflate compression for WebSocket connections to reduce bandwidth usage. The compression is applied per message while preserving context across messages, achieving significant data reduction (78% compression in tests) with a 15% CPU increase trade-off.
Key changes:
- Added permessage-deflate configuration to WebSocket server with optimized parameters
- Implemented WebSocketTracker class to monitor compression encoding and track bytes written
- Extended connection tracking throughout the request pipeline to support compression metrics
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
File | Description |
---|---|
socket-route.ts | Added compression tracking setup and byte counting for compressed streams |
types.ts | Extended stream payload interface to include WebSocket connection reference |
WebsocketDuplexConnection.ts | Implemented WebSocketTracker class and added compression detection logic |
ReactiveSocketRouter.ts | Configured permessage-deflate options and integrated connection tracking |
rich-fans-care.md | Added changeset documentation for the compression feature |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
Builds on #329.
This enables permessage-deflate for websockets connections, if the client indicates support. This is the only standardized compression extension supported for websockets.
Compression is per message (as implied by the name), but compression context is preserved across messages.
In my tests with synthetic data, 400MiB plaintext (bson) data is compressed into around 87MiB of compressed data, or 22% of the original size. This is still more than we get with zstd-compressed JSON data, but it is a significant reduction.
I'm seeing around 15% increase in CPU usage on the service when this is enabled, which is a worthwhile trade-off in my opinion.
This should work out-of-the-box with all major browsers - tested on Chrome and Firefox.
Enabling permessage-deflate is fairly simple - it primarily involved some performance testing of the various parameter combinations. The larger part of this PR is for tracking whether compression is used, and the actual number of bytes sent (for logging and metrics).