The EventSource specification describes the following:
The stream must then be parsed by reading everything line by line, with a U+000D U+000A (CRLF) character pair, a single U+000A (LF) character not preceded by a U+000D (CR) character, and a single U+000D (CR) character not followed by a U+000A (LF) character being the ways in which a line can end.
and
Lines must be processed, in the order they are received, as follows:
- If the line is empty (a blank line): dispatch the event
This means that if the event data is not followed by a blank line, the event is not dispatched.
The fix for the PHP code above is to make sure the data line is correctly terminated and a new empty line is introduced:
if($k>0)
echo "data:($k)\n\n";