PHPackages                             christoph-kluge/reactphp-http-response-compression-middleware - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. christoph-kluge/reactphp-http-response-compression-middleware

ActiveLibrary[HTTP &amp; Networking](/categories/http)

christoph-kluge/reactphp-http-response-compression-middleware
=============================================================

A response compression middleware for the ReactPHP HTTP-Server

1.0.0(7y ago)41.8k2[1 issues](https://github.com/christoph-kluge/reactphp-http-response-compression-middleware/issues)MITPHPPHP &gt;=5.6.0

Since Mar 3Pushed 7y ago3 watchersCompare

[ Source](https://github.com/christoph-kluge/reactphp-http-response-compression-middleware)[ Packagist](https://packagist.org/packages/christoph-kluge/reactphp-http-response-compression-middleware)[ RSS](/packages/christoph-kluge-reactphp-http-response-compression-middleware/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (1)Dependencies (6)Versions (2)Used By (0)

ReactPHP Response Compression Middleware
========================================

[](#reactphp-response-compression-middleware)

[![Build Status](https://camo.githubusercontent.com/5510229a780318d8b820dc3fbd54b8ed7f20d8d9da5c16aa0a5b945b1fd3522d/68747470733a2f2f7472617669732d63692e6f72672f6368726973746f70682d6b6c7567652f72656163747068702d687474702d726573706f6e73652d636f6d7072657373696f6e2d6d6964646c65776172652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/christoph-kluge/reactphp-http-response-compression-middleware)[![Total Downloads](https://camo.githubusercontent.com/6a0f39a7ab3db1680f7b11efb39dd875eb54f230e6779cba3ead33fe458c3cd0/68747470733a2f2f706f7365722e707567782e6f72672f6368726973746f70682d6b6c7567652f72656163747068702d687474702d726573706f6e73652d636f6d7072657373696f6e2d6d6964646c65776172652f646f776e6c6f616473)](https://packagist.org/packages/christoph-kluge/reactphp-http-response-compression-middleware)[![License](https://camo.githubusercontent.com/459a27d59225f60dbce99eefece7af88cb9a8fe0298d3eb5e78377ed8f711e56/68747470733a2f2f706f7365722e707567782e6f72672f6368726973746f70682d6b6c7567652f72656163747068702d687474702d726573706f6e73652d636f6d7072657373696f6e2d6d6964646c65776172652f6c6963656e7365)](https://packagist.org/packages/christoph-kluge/reactphp-http-response-compression-middleware)

Install
=======

[](#install)

To install via [Composer](http://getcomposer.org/), use the command below, it will automatically detect the latest version and bind it with `^`.

```
composer require christoph-kluge/reactphp-http-response-compression-middleware

```

This middleware will detect if the request is compressible and will compress the response body and add relevant headers to it. Heavy lifting is done by [clue/php-zlib-react](https://github.com/clue/php-zlib-react), thanks!

Usage
=====

[](#usage)

```
$server = new Server(new MiddlewareRunner([
    new ResponseCompressionMiddleware([
        new CompressionGzipHandler(),
        new CompressionDeflateHandler(),
    ]),
    function (ServerRequestInterface $request, callable $next) {
        return new Response(200, ['Content-Type' => 'application/json'], json_encode([
            'some' => 'nice',
            'json' => 'values',
        ]));
    },
]));
```

Response-detection for compressible mime/content-types
======================================================

[](#response-detection-for-compressible-mimecontent-types)

Default detection
-----------------

[](#default-detection)

The default handlers will use by default the `DefaultRegexDetecor` to identify compressible Content-Types by a set of regular expressions. Those regular expressions are:

```
// DefaultRegexDetector.php -> used in __construct()

new RegexDetector([
    '/^text\/[a-z-\+]+$/', // text/*
    '/^application\/json$/', // application/json
    '/^application\/xml$/', // application/xml
    '/^[a-z-\+]+\/[a-z-\+]+\+json$/', // */*+json
    '/^[a-z-\+]+\/[a-z-\+]+\+xml$/', // */*+xml
]);
```

Available Detectors
-------------------

[](#available-detectors)

There are currently the following available detectors:

- `ArrayDetector`: Which will accept an whitelist of mime-types to check against
- `RegexDetecor`: Which will accept an whitelist of regular expressions to check against

```
new CompressionGzipHandler(new ArrayDetector[
    'text/html',
]),
new CompressionGzipHandler(new RegexDetector[
    '/^text\/[a-z]+$/',
]),
```

Want to implement custom detectors?
-----------------------------------

[](#want-to-implement-custom-detectors)

If you would like to add a custom detection for the response mime type then you can simply pass an object as first parameter which implements the `MimeDetectorInterface`.

```
new CompressionGzipHandler(new class implements MimeDetectorInterface {
    public function isCompressible($mime) {
        // your custom magic here..
        return true;
    }
}),
```

License
=======

[](#license)

The MIT License (MIT)

Copyright (c) 2017 Christoph Kluge

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance15

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity55

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2633d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/23a3cbb346a78c868a78c52864ac0f3fe70a6cbabc488cefcf121e1d16006737?d=identicon)[christoph-kluge](/maintainers/christoph-kluge)

---

Top Contributors

[![christoph-kluge](https://avatars.githubusercontent.com/u/1446269?v=4)](https://github.com/christoph-kluge "christoph-kluge (17 commits)")[![WyriHaximus](https://avatars.githubusercontent.com/u/147145?v=4)](https://github.com/WyriHaximus "WyriHaximus (1 commits)")

---

Tags

compressiondeflategzipmiddlewarereactphp

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/christoph-kluge-reactphp-http-response-compression-middleware/health.svg)

```
[![Health](https://phpackages.com/badges/christoph-kluge-reactphp-http-response-compression-middleware/health.svg)](https://phpackages.com/packages/christoph-kluge-reactphp-http-response-compression-middleware)
```

###  Alternatives

[league/uri-interfaces

Common tools for parsing and resolving RFC3987/RFC3986 URI

536204.9M23](/packages/league-uri-interfaces)[shopify/shopify-api

Shopify API Library for PHP

4634.8M16](/packages/shopify-shopify-api)[laudis/neo4j-php-client

Neo4j-PHP-Client is the most advanced PHP Client for Neo4j

184616.9k31](/packages/laudis-neo4j-php-client)[http-interop/response-sender

A function to convert PSR-7 Response to HTTP output

46711.5k40](/packages/http-interop-response-sender)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)[mezzio/mezzio-authentication-oauth2

OAuth2 (server) authentication middleware for Mezzio and PSR-7 applications.

28483.0k2](/packages/mezzio-mezzio-authentication-oauth2)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
