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.
webware/sse 适用场景与选型建议
webware/sse 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「sse」 「Server-Sent Events」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webware/sse 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webware/sse 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webware/sse 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Instant real-time updates. Lightweight EventSource client receiving live messages via HTML5 Server-Sent Events (SSE). Fast stream processing built on top of ReactPHP's event-driven architecture.
A simple EventSource / SSE (Server-Sent Events) client.
An easy-to-use, object-oriented PHP library for Server-Sent Events
A Mercure Hub PHP implementation.
Alfabank REST API integration
Server Sent Event client
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 32
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2026-03-15