webware/sse
Composer 安装命令:
composer require 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.
README 文档
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.
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2026-03-15