webware / sse
A PHP library for Server-Sent Events (SSE) that provides a simple and efficient way to send real-time updates from the server to the client.
Requires
- php: ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0
- axleus/axleus-message: dev-master
- laminas/laminas-diactoros: ^3.0
- laminas/laminas-httphandlerrunner: ^2.0
- psr/container: ^2.0
- psr/http-message: ^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- laminas/laminas-servicemanager: ^4.0
- laminas/laminas-view: ^3.0
- phpstan/phpstan: ^2.1
- phpstan/phpstan-phpunit: ^2.0
- phpunit/phpunit: ^11.5.42
- roave/security-advisories: dev-master
- webware/coding-standard: ^0.1.0
Suggests
- laminas/laminas-servicemanager: Provides ServiceManager integration for Mezzio applications; required to use the bundled ConfigProvider with laminas-servicemanager-based containers
This package is auto-updated.
Last update: 2026-03-16 11:25:44 UTC
README
A Server-Sent Events (SSE) notification component for Mezzio applications built on the Laminas ecosystem.
- PSR-7 native —
SseResponseextendsLaminas\Diactoros\Response - EmitterStack-aware —
SseEmitterimplementsEmitterInterfaceand returnsfalsefor non-SSE responses, lettingSapiEmitterhandle the rest - PSR-15 request handler —
NotificationHandlerreads queued messages and streams them as named SSE events - Laminas View integration — each message is rendered through a configurable
.phtmlpartial before being sent - htmx v2 compatible — named events map directly to
sse-swaptargets in htmx'ssseextension - Mezzio router integration —
RouteProviderregisters/notificationsand its middleware pipeline automatically - PSR-11 container —
ConfigProviderwires all services;laminas-servicemanageris required at runtime
Requirements
- PHP 8.2–8.5
laminas/laminas-diactoros^3.0laminas/laminas-httphandlerrunner^2.0laminas/laminas-servicemanager^4.0laminas/laminas-viewaxleus/message(forSystemMessengerInterfaceandMessageMiddleware)mezzio/mezzio(forRouteProviderInterface)
Quick Start
1. Install
composer require webware/sse
2. Register ConfigProvider
Add Webware\SSE\ConfigProvider to your config aggregator. This registers all factories, the EmitterStack delegator, the route provider, and the view templates.
// config/config.php use Laminas\ConfigAggregator\ConfigAggregator; use Webware\SSE\ConfigProvider; return (new ConfigAggregator([ ConfigProvider::class, // your other providers … ]))->getMergedConfig();
3. Ensure SapiEmitter is on the stack (optional advanced usage)
ConfigProvider registers a delegator that pushes SseEmitter onto the EmitterStack. You must still push SapiEmitter below it so that ordinary responses are handled:
// register the delegator factory in ConfigProvider public function getDependencies(): array { return [ 'delegators' => [ EmitterInterface::class => [ Container\EmitterStackDelegatorFactory::class, ], ], //.... ]; }
4. Register routes
ConfigProvider declares RouteProvider under router.route-providers. If your Mezzio bootstrap processes that key automatically, no extra step is needed.
For manual registration:
// config/routes.php use Webware\SSE\RouteProvider; (new RouteProvider())->registerRoutes($app, $factory);
This registers GET /notifications → SessionMiddleware → MessageMiddleware → NotificationHandler.
5. Wire up the HTML page with htmx
Include the htmx sse extension and connect to the /notifications endpoint. Each incoming SSE event is swapped into the matching sse-swap target:
<script src="https://unpkg.com/htmx.org@2"></script> <script src="https://unpkg.com/htmx-ext-sse@2"></script> <div hx-ext="sse" sse-connect="/notifications"> <div id="notifications-info" sse-swap="info" hx-swap="beforeend"></div> <div id="notifications-warning" sse-swap="warning" hx-swap="beforeend"></div> <div id="notifications-error" sse-swap="error" hx-swap="beforeend"></div> <div id="notifications-message" sse-swap="message" hx-swap="beforeend"></div> </div>
When NotificationHandler processes a request it renders each queued message through its corresponding sse::<level> partial and emits the HTML fragment as a named SSE event. htmx routes the event to the matching swap target.
6. (Optional) Emit an event manually
If you need to emit a one-off SSE response from your own handler:
use Webware\SSE\Event; use Webware\SSE\SseResponse; $event = new Event( data: '<article class="notification info">Deploy complete.</article>', event: 'info', id: 'deploy-123', ); return new SseResponse($event);
Documentation
| Document | Description |
|---|---|
| docs/event.md | EventInterface contract and Event value object — constructor, constants, wire-format output |
| docs/sse-response.md | SseResponse — accepted body types, default headers |
| docs/sse-emitter.md | SseEmitter, SseEmitterFactory, EmitterStackDelegatorFactory, stack ordering |
| docs/notification-handler.md | NotificationHandler request flow, request attributes, NotificationHandlerFactory |
| docs/route-provider.md | RouteProvider middleware pipeline, RouteProviderFactory |
| docs/config-provider.md | ConfigProvider — full config key reference for dependencies, router, and templates |
| docs/templates.md | Template keys, available variables, overriding defaults |
License
BSD-3-Clause. See LICENSE.