PHPackages                             milpa/mercure - 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. milpa/mercure

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

milpa/mercure
=============

Mercure hub publisher for the Milpa PHP framework: self-signed JWT generation and SSE publish over cURL.

v0.1.0(3d ago)01Apache-2.0PHP &gt;=8.3

Since Jul 7Compare

[ Source](https://github.com/getmilpa/mercure)[ Packagist](https://packagist.org/packages/milpa/mercure)[ RSS](/packages/milpa-mercure/feed)WikiDiscussions Synced today

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

 [   ![Milpa](https://raw.githubusercontent.com/getmilpa/core/main/art/lockup/milpa-lockup-v-color-light.svg)  ](https://github.com/getmilpa)

Milpa Mercure
=============

[](#milpa-mercure)

> The **Mercure hub publisher** for the Milpa PHP framework, with **zero package dependencies**. One class, `MercureService`, mints self-signed HS256 JWTs by hand and pushes real-time updates to a [Mercure](https://mercure.rocks/) hub over `curl_*` directly — no HTTP client abstraction, no `milpa/core` at runtime.

[![CI](https://github.com/getmilpa/mercure/actions/workflows/ci.yml/badge.svg)](https://github.com/getmilpa/mercure/actions/workflows/ci.yml)[![Packagist](https://camo.githubusercontent.com/111aad90932db3b831a4435eb045e586ce0cbaca698c35e34b9d427e8c0712d1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d696c70612f6d6572637572652e737667)](https://packagist.org/packages/milpa/mercure)[![PHP](https://camo.githubusercontent.com/ca03f11ea27dac4dedc8ad56a7bdfc4a9ff5feb825055f9d2983616115076607/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d254532253839254135253230382e332d3737376262342e737667)](https://www.php.net/)[![License](https://camo.githubusercontent.com/798509b4df525f56802b56f8096862487f08023e3d7561c68656f8dab10d0d6e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4170616368652d2d322e302d626c75652e737667)](LICENSE)[![Docs](https://camo.githubusercontent.com/c6dc6a3411e15b0ac7cc4583e8e6a8144181caedb82f5d98753353decda06d77/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f646f63732d4150492532307265666572656e63652d626c75652e737667)](https://getmilpa.github.io/mercure/)

`milpa/mercure` is the smallest possible seam onto a [Mercure hub](https://mercure.rocks/): sign a publisher JWT, `POST` a topic + JSON payload to the hub's `/.well-known/mercure`endpoint, and mint short-lived subscriber JWTs for the browser's `EventSource` connection. **No ORM, no event dispatcher, no framework coupling** — construct it with four strings and call `publish()`.

Install
-------

[](#install)

```
composer require milpa/mercure
```

Quick example
-------------

[](#quick-example)

```
use Milpa\Mercure\MercureService;

$mercure = new MercureService(
    hubUrl: 'https://hub.internal/.well-known/mercure',      // server-side POST target
    publicUrl: 'https://hub.example.com/.well-known/mercure', // what the browser connects to
    publisherKey: $_ENV['MERCURE_PUBLISHER_JWT_KEY'],
    subscriberKey: $_ENV['MERCURE_SUBSCRIBER_JWT_KEY'],
);

// Server-side: push an update. Mints a 60s publisher JWT, POSTs form-encoded
// topic + JSON data, throws RuntimeException on cURL failure or HTTP >= 400.
$mercure->publish('conversations/42/messages', [
    'event' => 'message.created',
    'body' => 'hello',
]);

// Browser-side: mint a 5-minute subscriber JWT scoped to the topics this
// visitor may listen to, and hand the browser the public URL to connect to.
$jwt = $mercure->generateSubscriberJwt(['conversations/42/messages']);
$publicUrl = $mercure->getPublicUrl();
```

The browser then opens `new EventSource(publicUrl + '?topic=...', { withCredentials: true })`with `$jwt` set as the `mercureAuthorization` cookie — standard Mercure subscribe flow, nothing Milpa-specific about it.

What it does — and doesn't
--------------------------

[](#what-it-does--and-doesnt)

- **`publish(string $topic, array $data)`** — signs a 1-minute publisher JWT scoped to `$topic`, form-encodes `topic` + `json_encode($data)`, and `POST`s it to `hubUrl` with `Authorization: Bearer `. A `curl_init()` failure, a `curl_exec()` failure, or an HTTP status `>= 400` all throw `RuntimeException` — `publish()` never fails silently.
- **`generateSubscriberJwt(array $topics)`** — a 5-minute HS256 JWT whose payload is `{"mercure":{"subscribe":$topics},"exp":...}`, signed with `subscriberKey`. This is the token a browser presents to subscribe; it grants no publish access.
- **`getPublicUrl()`** — returns the `publicUrl` given to the constructor, unmodified. Kept separate from `hubUrl` because the two are commonly different: the server publishes to an internal/Docker-network hostname, while the browser subscribes through a public one.

Both JWTs are HS256, hand-assembled from `header.payload.signature` with `hash_hmac()` + base64url — no JWT library dependency. There is no retry, no queue, and no HTTP client abstraction to inject: `publish()` talks to `curl_*` directly, by design (see [Requirements](#requirements)).

Requirements
------------

[](#requirements)

- PHP **≥ 8.3** with the **cURL extension** enabled (`ext-curl`)
- Nothing else — `milpa/mercure` has no package dependencies, Milpa or otherwise

Documentation
-------------

[](#documentation)

**Full API reference: [getmilpa.github.io/mercure](https://getmilpa.github.io/mercure/)** — generated straight from the source DocBlocks and dressed with the Milpa design system.

Contributing
------------

[](#contributing)

Contributions are welcome — see [CONTRIBUTING.md](CONTRIBUTING.md). Please report security issues via [SECURITY.md](SECURITY.md), and note that this project follows a [Code of Conduct](CODE_OF_CONDUCT.md).

License
-------

[](#license)

[Apache-2.0](LICENSE) © TeamX Agency.

---

Milpa is designed, built, and maintained by **[TeamX Agency](https://teamx.agency/?utm_source=github&utm_medium=readme&utm_campaign=milpa&utm_content=mercure)**.

###  Health Score

37

—

LowBetter than 81% of packages

Maintenance99

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

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

3d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1993784?v=4)[rodrigomx](/maintainers/rodrigomx)[@rodrigomx](https://github.com/rodrigomx)

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/milpa-mercure/health.svg)

```
[![Health](https://phpackages.com/badges/milpa-mercure/health.svg)](https://phpackages.com/packages/milpa-mercure)
```

###  Alternatives

[php-http/cache-plugin

PSR-6 Cache plugin for HTTPlug

25126.1M82](/packages/php-http-cache-plugin)[rdkafka/rdkafka

A PHP extension for Kafka

2.2k24.3k1](/packages/rdkafka-rdkafka)[httpsoft/http-message

Strict and fast implementation of PSR-7 and PSR-17

87965.9k119](/packages/httpsoft-http-message)[serpapi/google-search-results-php

Get Google, Bing, Baidu, Ebay, Yahoo, Yandex, Home depot, Naver, Apple, Duckduckgo, Youtube search results via SerpApi.com

69127.2k](/packages/serpapi-google-search-results-php)[swoft/websocket-server

swoft websocket server component

16135.7k5](/packages/swoft-websocket-server)[thesis/nats

Async (fiber based) client for Nats.

744.4k](/packages/thesis-nats)

PHPackages © 2026

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