CodeIgniter Forums
How to set header content-type text/event-stream? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forum-31.html)
+--- Thread: How to set header content-type text/event-stream? (/thread-72525.html)



How to set header content-type text/event-stream? - titounnes - 01-02-2019

Hi, all.
I want to create data text/event-stream for Server Sent Event. I wrote code like this.
PHP Code:
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
echo 
"Data:test\n\n"
but, the results I get are like this.
HTTP/1.1 200 OK
Server: nginx/1.14.0 (Ubuntu)
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Cache-Control: no-cache
Cache-control: no-store, max-age=0, no-cache
Date: Thu, 03 Jan 2019 01:39:01 GMT

data: Hallo:  

Can anyone help me, please?


RE: How to set header content-type text/event-stream? - kilishan - 01-02-2019

You should always use the Response object that is in every controller to set the headers instead of setting them directly now. Something like:

Code:
$this->response->setHeader('Content-Type', 'text/event-stream');


By default - caching is turned off through the headers so you don't have to specify that. Unless changed it sets Cache-Control to no-store, max-age=0, no-cache.


RE: How to set header content-type text/event-stream? - titounnes - 01-03-2019

[quote='kilishan' pid='360708' dateline='1546488262']
You should always use the Response object that is in every controller to set the headers instead of setting them directly now. Something like:

Code:
$this->response->setHeader('Content-Type', 'text/event-stream');
Thanks for your reply. I will try it.