承接 hosmelq/sse 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

hosmelq/sse

Composer 安装命令:

composer require hosmelq/sse

包简介

A PHP library for consuming Server-Sent Events (SSE) streams with WHATWG compliance.

README 文档

README

Consume Server-Sent Events (SSE) with PHP.

Features

Requirements

  • PHP 8.2 or higher.

Installation

Install via Composer:

composer require hosmelq/sse

Quick Start

<?php

use HosmelQ\SSE\Client;

$client = new Client();

$eventSource = $client->get('https://example.com/events');

foreach ($eventSource->events() as $event) {
    echo "Data: {$event->data}\n";
    echo "Event: {$event->event}\n";
    echo "ID: {$event->id}\n";
}

Usage

Creating a Client

The Client class is the main entry point for connecting to SSE endpoints:

use HosmelQ\SSE\Client;
use GuzzleHttp\Client as HttpClient;

// Using a default HTTP client
$client = new Client();

// Using a custom HTTP client
$httpClient = new HttpClient(['timeout' => 30]);
$client = new Client($httpClient);

Connecting to SSE Endpoints

The client supports both GET and POST methods:

// GET request (most common for SSE)
$eventSource = $client->get('https://api.example.com/stream');

// POST request with data
$eventSource = $client->post('https://api.example.com/stream', [
    'json' => ['subscribe' => 'user-123']
]);

// Using generic connect method
$eventSource = $client->connect('GET', 'https://api.example.com/stream', [
    'read_timeout' => 30,
    'timeout' => 60,
]);

Processing Events

foreach ($eventSource->events() as $event) {
    echo $event->data;  // Event data
    echo $event->event; // Event type (default: "message")
    echo $event->id;    // Event ID (optional)
    echo $event->retry; // Retry interval (optional)
}

Event Types and Data

Events follow the SSE specification with these fields:

// Example event from server:
// data: {"message": "Hello World", "timestamp": 1640995200}
// event: notification
// id: 123
// retry: 5000

$event = /* ... received event ... */;

echo $event->data;  // '{"message": "Hello World", "timestamp": 1640995200}'
echo $event->event; // "notification"
echo $event->id;    // "123"
echo $event->retry; // 5000

// Parse JSON data
$notification = $event->json();

echo $notification['message']; // "Hello World"

Configuration Options

$eventSource = $client->get('https://api.example.com/stream', [
    'headers' => ['Authorization' => 'Bearer token'],
    'query' => ['channel' => 'notifications'],
    'read_timeout' => 30,
    'timeout' => 60,
]);

Exception Handling

The library provides a clean exception hierarchy for different types of errors:

SSEException (base class)
├── SSEConnectionException (connection/transport errors)
└── SSEProtocolException (protocol violations)

Exception Types

SSEConnectionException - Thrown when connection or transport errors occur.

SSEProtocolException - Thrown when the server violates the SSE protocol.

SSEException - Base class that catches all SSE-related errors.

Error Handling Examples

<?php

use HosmelQ\SSE\Client;
use HosmelQ\SSE\SSEException;
use HosmelQ\SSE\SSEConnectionException;
use HosmelQ\SSE\SSEProtocolException;

$client = new Client();

try {
    $eventSource = $client->get('https://api.example.com/stream');
    
    foreach ($eventSource->events() as $event) {
    }
} catch (SSEConnectionException $e) {
    echo "Connection failed: " . $e->getMessage();
} catch (SSEProtocolException $e) {
    echo "Protocol error: " . $e->getMessage();
} catch (SSEException $e) {
    echo "SSE Error: " . $e->getMessage();
}

Last Event ID

Track the last received event ID for reconnection scenarios:

$eventSource = $client->get('https://api.example.com/stream');

try {
    foreach ($eventSource->events() as $event) {
    }
} catch (Exception $e) {
    $lastEventId = $eventSource->getLastEventId();

    $eventSource = $client->get('https://api.example.com/stream', [
        'headers' => ['Last-Event-ID' => $lastEventId]
    ]);
}

Testing

composer test

Changelog

See CHANGELOG.md for a list of changes.

Credits

Based on:

License

The MIT License (MIT). Please see License File for more information.

hosmelq/sse 适用场景与选型建议

hosmelq/sse 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.64k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2025 年 07 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「stream」 「event-source」 「server-sent-events」 「real-time」 「sse」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 hosmelq/sse 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hosmelq/sse 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 6.64k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 37
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-07-13