Skip to content

Instantly share code, notes, and snippets.

Created May 26, 2010
What would you like to do?
<script src="event-source.js"></script>
document.addEventListener('DOMContentLoaded', function () {
var eventSrc = new EventSource('events.php');
eventSrc.addEventListener('open', function (event) {
eventSrc.addEventListener('message', function (event) {
}, false);
header("Content-Type: text/event-stream\n\n");
// despite not having the while(true){}
// this seems to repeat pushing messages to the client
echo 'data: ' . time() . "\n";
Copy link

The reason it repeats is that the connection is closed after the script terminates. The EventSource tries to reconnect, gets one message and the connection is terminated again. Repeat. Not really SSE but polling... looping and holding the connection open is the point of SSE, as it removes the HTTP overhead of the polling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment