webware/sse 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 nativeSseResponse extends Laminas\Diactoros\Response
  • EmitterStack-awareSseEmitter implements EmitterInterface and returns false for non-SSE responses, letting SapiEmitter handle the rest
  • PSR-15 request handlerNotificationHandler reads queued messages and streams them as named SSE events
  • Laminas View integration — each message is rendered through a configurable .phtml partial before being sent
  • htmx v2 compatible — named events map directly to sse-swap targets in htmx's sse extension
  • Mezzio router integrationRouteProvider registers /notifications and its middleware pipeline automatically
  • PSR-11 containerConfigProvider wires all services; laminas-servicemanager is required at runtime

Requirements

  • PHP 8.2–8.5
  • laminas/laminas-diactoros ^3.0
  • laminas/laminas-httphandlerrunner ^2.0
  • laminas/laminas-servicemanager ^4.0
  • laminas/laminas-view
  • axleus/message (for SystemMessengerInterface and MessageMiddleware)
  • mezzio/mezzio (for RouteProviderInterface)

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 /notificationsSessionMiddleware → 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 webware/sse 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2026-03-15