Skip to content

zstd-stream support#1433

Open
valzargaming wants to merge 5 commits intomasterfrom
zstd
Open

zstd-stream support#1433
valzargaming wants to merge 5 commits intomasterfrom
zstd

Conversation

@valzargaming
Copy link
Copy Markdown
Member

@valzargaming valzargaming commented Nov 21, 2025

https://discord.com/developers/docs/events/gateway#zstdstream
https://github.com/kjdev/php-ext-zstd
https://phpext.phptools.online/extension/zstd-334

This pull request adds support for Zstandard (zstd) compression in addition to the existing zlib compression for WebSocket payloads in the Discord client. If the ext-zstd extension is available, the client will now prefer zstd-stream compression, falling back to zlib-stream if not. The changes include dependency updates, new decompression logic, and improved logging for compression handling.

Compression support improvements:

  • Added ext-zstd as a suggested dependency in composer.json for Zstandard compression support.
  • Updated the Discord client to initialize and use a Zstd decompression context (ZstdContext) if the extension is available, falling back to zlib decompression otherwise. [1] [2] [3]

WebSocket message handling:

  • Modified the WebSocket message handler to decompress binary payloads using Zstd if available, or zlib otherwise, and added error logging for failed decompression attempts.

Logging and diagnostics:

  • Added debug logging to indicate which compression method (zstd-stream or zlib-stream) is being used during connection parameter setup.

@valzargaming valzargaming linked an issue Nov 21, 2025 that may be closed by this pull request
@valzargaming valzargaming requested a review from a team November 21, 2025 19:12
Copilot AI review requested due to automatic review settings April 4, 2026 10:52
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds support for Discord Gateway zstd-stream transport compression (preferring zstd when available, with fallback to existing zlib-stream) and updates dependency suggestions accordingly.

Changes:

  • Add optional ext-zstd Composer suggestion.
  • Initialize a zstd decompression context when ext-zstd is available and request zstd-stream compression.
  • Decompress binary WebSocket payloads using zstd when selected, otherwise keep existing zlib-stream behavior.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/Discord/Discord.php Adds zstd decompression context setup, selects zstd-stream in gateway params when available, and handles zstd-decompressed binary payloads.
composer.json Suggests ext-zstd for optional Zstandard compression support.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 1671 to +1678
if ($this->useTransportCompression) {
if ($this->zlibDecompressor = inflate_init(ZLIB_ENCODING_DEFLATE)) {
// Prefer zstd-stream if available (better compression), fallback to zlib-stream
if (extension_loaded('zstd') && ($this->zstdDecompressor = uncompress_init())) {
$params['compress'] = 'zstd-stream';
$this->logger->debug('using zstd-stream compression');
} elseif ($this->zlibDecompressor = inflate_init(ZLIB_ENCODING_DEFLATE)) {
$params['compress'] = 'zlib-stream';
$this->logger->debug('using zlib-stream compression');
Copy link

Copilot AI Apr 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New zstd-stream selection and decompression behavior isn’t covered by automated tests. Consider adding a unit test that verifies (a) buildParams prefers zstd-stream when available and falls back to zlib-stream otherwise, and (b) handleWsMessage routes binary payloads through the selected decompressor (you may need to wrap the extension checks/calls to make this testable without ext-zstd in CI).

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

zstd-stream compression support

2 participants