-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: SSEResponse class for streaming Server-Side Events
#9931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 4.8
Are you sure you want to change the base?
Conversation
datamweb
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CodeIgniter4/app/Config/Events.php
Line 28 in 1b41358
| if (ini_get('zlib.output_compression')) { |
Is it not disabled by default?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that app/Config/Events.php is user-configurable, and someone could completely remove thezlib.output_compression check. That's why it's safer to keep it here. The cost is negligible.
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: