'How to configure apache to work with SSE?
I have FastCGI application, that implements SSE (server-sent events).
The local test server is lighttpd and in order to make it to work properly, I needed to set: server.stream-response-body = 1 in the configuration file.
But on the production there is an Apache server and it does not works properly, just like lighttpd before the above setting.
The effect is that the front-end connects, but the server does not respond until some big amount of data is collected. Obviously there is some buffering in the web server that ignores the text/event-stream header.
The script send the only header:
Content-Type: text/event-stream
So, how to configure apache to send the stream immediately instead of buffering it?
Is it something in the .htaccess file or I should send some special headers?
Solution 1:[1]
the server does not respond until some big amount of data is collected
Yes, that's one of the strange behaviours that everyone faces with SSE - and PHP.
You need to add dummy data so that the PHP flush works.
I use this function to add the required data to reach the data length for the flush:
function sse_out($string)
{
// start buffer if not already
if (ob_get_level() == 0)
{
ob_start();
}
echo "data: ".$string."\n\n";
if (strlen($string) < 4096)
{
echo str_pad(' ', 4096)."\n";
}
ob_flush();
flush();
}
Send any data with calling sse_out('Hello there.');
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Avatar |
