Search by

raneomik / nette-mercure

raneomik

🚀 Nette Mercure Extension: nette wrapper around https://github.com/symfony/mercure

Package info

github.com/raneomik/nette-mercure

pkg:composer/raneomik/nette-mercure

Statistics

Installs: 107

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.7.1 2026-11-15 00:35 UTC

This package is auto-updated.

Last update: 2026-11-20 00:20:13 UTC


README

.github/workflows/ci.yml .github/workflows/coverage.yml codecov Mutation Testing Mutation testing badge

Work In Progress

🚀 Nette Mercure Extension: wrapper for symfony/mercure to use Mercure in Nette.

Mercure is a protocol allowing to push data updates to web browsers and other HTTP clients in a convenient, fast, reliable and battery-efficient way. It is especially useful to publish real-time updates of resources served through web APIs, to reactive web and mobile apps.

Requirements

  • PHP 8.3 or newer
  • nette/di 3.2, nette/http 3.4 or newer
  • A Mercure hub: the one built in FrankenPHP, or a standalone one
  • Optional:
Package For
latte/latte templates in broadcasts, {mercure()} and {mercureJWTToken()}
nette/application subscriber, authorization and discovery services
tracy/tracy debug panel

Getting started

composer require raneomik/nette-mercure

Register the extension and configure the hub:

extensions:
	mercure: Raneomik\NetteMercure\Bridge\DI\MercureExtension(%debugMode%)

mercure:
	url: 'https://example.com/.well-known/mercure'
	jwt:
		key: %mercureJwtKey% # openssl rand -hex 32, the same key as the hub's

Publish:

use Raneomik\NetteMercure\BroadcasterInterface;

final class SomeService
{
	public function __construct(
		private BroadcasterInterface $broadcaster,
	) {
	}

	public function someAction(): void
	{
		$this->broadcaster->broadcast(
			topics: 'test-topic',
			data: 'Hello Nette from Mercure!',
		);
	}
}

Subscribe:

<div class="mercure-container">Waiting for updates...</div>

<script type="module">
	const eventSource = new EventSource({mercure('test-topic')});

	eventSource.onmessage = event => {
		document.querySelector('.mercure-container').textContent = event.data;
	};
</script>

Documentation

  • How-to guides: signing key, several hubs, templates, private updates, subscribing (JWT, cookie, discovery), Turbo Streams, locales, hot reload
  • Reference: configuration, PHP services, Latte functions
  • Debugging: Tracy panel, debug bus

How it works

  • The hub implementation is picked at runtime: the FrankenPHP built-in hub when mercure_publish() exists (Symfony\Component\Mercure\FrankenPhpHub), HTTP to url otherwise (Symfony\Component\Mercure\Hub). The same compiled container works in FrankenPHP workers and in CLI commands, as long as url is reachable from the CLI.
  • A publication is rendered once (once per locale with a translator) and sent as is to every subscriber of the topic.

Development

composer install && composer ci

Resources

Known issues

  • The anonymous option of Mercure in the Caddy configuration seems to work only with Symfony\Component\Mercure\FrankenPhpHub and the FrankenPHP built-in mercure_publish function. HttpClient shows errors such as "405 Method Not Allowed" in this case.

License

MIT