Download the PHP package bentools/mercure-php-hub without Composer
On this page you can find all versions of the php package bentools/mercure-php-hub. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mercure-php-hub
Mercure PHP Hub
This POC was a PHP implementation of the Mercure Hub Specification.
This repository will no longer be maintained, as I recently released Freddie, which is a brand new implementation leveraging Framework X (still using ReactPHP under the hood).
Installation
PHP 7.4+ (and Redis, or a Redis instance, if using the Redis transport) is required to run the hub.
Usage
You can use environment variables (UPPER_SNAKE_CASE) to replace CLI options for better convenience.
The hub will also check for the presence of a /etc/mercure/mercure.env
file,
then make use of the Symfony DotEnv component to populate variables.
Check out configuration.php for full configuration options.
Advantages and limitations
This implementation does not provide SSL nor HTTP2 termination, so you'd better put a reverse proxy in front of it.
Example with nginx:
By default, the hub will run as a simple event-dispatcher. It can fit common needs for a basic usage, but is not scalable (opening another process won't share the same event emitter).
On the other hand, you can launch the hub on multiple ports and/or multiple servers with a Redis transport (as soon as they share the same Redis instance) and leverage a load-balancer to distribute the traffic. This is currently not possible with the open-source Go implementation of the hub because of concurrency restrictions on the bolt transport.
Benchmarks
Simulated 1 / 100 /1000 subscribers on server A, 1 publisher blasting messages on server B, Hub on server C (and D for the latter).
Implementation | Transport | Servers | Nodes | 1 subscriber | 100 subscribers | 1000 subscribers |
---|---|---|---|---|---|---|
Mercure.rocks GO Hub | Bolt | 1 | 1 | 361 / 286 | 129 / 4989 | 142 / 682 |
ReactPHP implementation | PHP | 1 | 1 | 860 / 295 | 519 / 4526 | 45 / 322 |
ReactPHP implementation | Redis (local) | 1 | 1 | 1548 / 411 | 393 / 5861 | 112 / 777 |
ReactPHP implementation | Redis (local) | 1 | 4 | 108 / 76 | 61 / 2852 | 61 / 688 |
ReactPHP implementation | Redis (shared) | 2 | 8 | 3035 / 144 | 1183 / 7864 | 708 / 2698 |
Units are POSTs (publish) / s
/ Received events / s
(total for all subscribers).
Nodes are the total number of ReactPHP open ports.
Hub was hosted on cheap server(s): 2GB / 2 CPU VPS (Ubuntu 20.04). You could probably reach a very high level of performance with better-sized servers and dedicated CPUs.
Feature coverage
Feature | Covered |
---|---|
JWT through Authorization header |
✅ |
JWT through mercureAuthorization Cookie |
✅ |
Different JWTs for subscribers / publishers | ✅ |
Allow anonymous subscribers | ✅ |
CORS | ✅ |
Private updates | ✅ |
URI Templates for topics | ✅ |
Health check endpoint | ✅ |
HMAC SHA256 JWT signatures | ✅ |
RS512 JWT signatures | ✅ |
Environment variables configuration | ✅ |
Custom message IDs | ✅ |
Last event ID | ✅️ (except: earliest on REDIS transport) |
Customizable event type | ✅️ |
Customizable retry directive |
✅️ |
Logging | ❌ (WIP)️ |
Metrics | ❌ (WIP)️ |
Subscription events | ❌️ |
Subscription API | ❌️ |
Configuration w/ config file | ❌️ |
Payload | ❌️ |
Heartbeat | ❌️ |
Forwarded / X-Forwarded-For headers |
❌️ |
Alternate topics | ❌️ |
Additional features
This implementation provides features which are not defined in the original specification.
Subscribe / Publish topic exclusions
Mercure leverages URI Templates to grant subscribe and/or publish auhorizations on an URI pattern basis:
However, denying access to a specific URL matching an URI template requires you to explicitely list authorized items:
When dealing with thousands of possibilities, it can quicky become a problem. The Mercure PHP Hub allows you to specify
denylists through the publish_exclude
and subscribe_exclude
keys, which accept any topic selector:
Json Web Token Generator
You can generate a JWT to use on the hub from the command-line:
It will ask you interactively what topic selectors you want to allow/deny for publishing/subscribing, and ask you for an optional TTL.
If you want a raw output (to pipe the generated JWT for instance), use the --raw
option.
To disable interaction, you can use the following example:
It will use your JWT keys environment variables, or you can use the --jwt-key
, --publisher-jwt-key
, --subscriber-jwt-key
options.
For a full list of available options, run this:
Tests
This project is covered with Pest tests. Coverage has to be improved: feel free to contribute.
Contribute
If you want to improve this project, feel free to submit PRs:
- CI will yell if you don't follow PSR-12 coding standards
- In the case of a new feature, it must come along with tests
- PHPStan analysis must pass at level 5
You can run composer ci:check
before committing to ensure all CI requirements are successfully met.
License
GNU General Public License v3.0.
All versions of mercure-php-hub with dependencies
ext-json Version *
ext-mbstring Version *
ext-pcntl Version *
bentools/psr7-request-matcher Version ^1.1
bentools/querystring Version ^1.0
clue/block-react Version ^1.4
clue/redis-react Version ^2.4
lcobucci/jwt Version 3.3.*
predis/predis Version ^1.1
psr/http-server-middleware Version ^1.0
psr/log Version ^1.1
ramsey/uuid Version ^4.0
react/http Version ^1.0.0
rize/uri-template Version ^0.3.2
symfony/console Version ^5.2
symfony/dotenv Version ^5.2
symfony/flex Version ^1.11.0
symfony/framework-bundle Version ^5.2
symfony/string Version ^5.2
symfony/yaml Version ^5.2