feat: SSEResponse class for streaming Server-Side Events#9931
feat: SSEResponse class for streaming Server-Side Events#9931michalsn wants to merge 5 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.
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!
| { | ||
| // Turn off output buffering completely, even if php.ini output_buffering is not off | ||
| if (ENVIRONMENT !== 'testing') { | ||
| set_time_limit(0); |
There was a problem hiding this comment.
SSE connections are long-lived by design - server keeps the connection open indefinitely, streaming events over time (seconds, minutes, hours). Default max_execution_time (typically 30 seconds) would kill the script mid-stream.
Co-authored-by: John Paul E. Balandan, CPA <paulbalandan@gmail.com>
| // Turn off output buffering completely, even if php.ini output_buffering is not off | ||
| if (ENVIRONMENT !== 'testing') { | ||
| set_time_limit(0); | ||
| ini_set('zlib.output_compression', 'Off'); |
There was a problem hiding this comment.
CodeIgniter4/app/Config/Events.php
Line 28 in 1b41358
Is it not disabled by default?
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: