feat: SSEResponse class for streaming Server-Side Events#9931
feat: SSEResponse class for streaming Server-Side Events#9931michalsn wants to merge 4 commits intocodeigniter4:4.8from
SSEResponse class for streaming Server-Side Events#9931Conversation
datamweb
left a comment
There was a problem hiding this comment.
First of all, I’m really happy to see CodeIgniter continuing its path towards modernization.
I had a couple of thoughts regarding the example provided in the documentation. While the current example is excellent from a PHP developer’s perspective and clearly demonstrates the API usage, I believe it may be less impactful for developers who are focused on building end‑user products.
Perhaps a more product‑oriented example could better showcase the real value of this class — for example, a small notification system or a real‑time update scenario. This could help readers more intuitively grasp the practical power and use cases enabled by this addition.
| * | ||
| * Each line becomes "{prefix}: {line}\n", terminated by an extra "\n". | ||
| */ | ||
| private function formatMultiline(string $prefix, string $value): string |
There was a problem hiding this comment.
formatMultiline() and write() appear to be natural extension
boundaries (I/O and formatting).
Would making them protected help avoid duplication or framework
forking when custom SSE behavior is needed?
| private function formatMultiline(string $prefix, string $value): string | |
| protected function formatMultiline(string $prefix, string $value): string |
There was a problem hiding this comment.
Formatting methods are dictated by the spec, not by application needs. I don't see a scenario where this would need modification.
| /** | ||
| * Write raw SSE output and flush. | ||
| */ | ||
| private function write(string $output): bool |
There was a problem hiding this comment.
| private function write(string $output): bool | |
| protected function write(string $output): bool |
There was a problem hiding this comment.
Again, I can't imagine a legitimate scenario where this may be handy. If a concrete need comes up later, promoting private to protected is a non-breaking change. Going the other direction isn't.
Co-authored-by: Pooya Parsa <pooya_parsa_dadashi@yahoo.com>
|
@datamweb Thanks for the feedback. I've added a "live notifications" example to better illustrate the idea behind SSE. |
datamweb
left a comment
There was a problem hiding this comment.
Thank you for addressing this so thoroughly.
Let's see what others say!
Description
This PR introduces
SSEResponse, a new response class for streaming Server-Sent Events over HTTP. The class providesevent(),comment(), andretry()methods for sending SSE fields to the client. Array data passed toevent()is automatically JSON-encoded. The response also handles output buffering, session closing, and appropriate header management.SSEResponseimplements theNonBufferedResponseInterface, which is shared withDownloadResponse. Guard checks inCodeIgniter,PageCache, and theToolbarthat previously referencedDownloadResponsedirectly now rely on this interface instead. This ensures that both download and SSE responses are correctly excluded from page caching, toolbar injection, and body rendering.Checklist: