Note that there are some explanatory texts on larger screens.

plurals
  1. POHTML5 Server-Sent Events prototyping - ambiguous error and repeated polling?
    primarykey
    data
    text
    <p>I'm trying to get to grips with Server-Side Events as they fit my requirements perfectly and seem like they should be simple to implement, however I can't get past a vague error and what looks like the connection repeatedly being closed and re-opened. Everything I have tried is based on <a href="http://www.html5rocks.com/en/tutorials/eventsource/basics" rel="noreferrer">this</a> and other tutorials.</p> <p>The PHP is a single script:</p> <pre><code>&lt;?php header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); function sendMsg($id, $msg) { echo "id: $id" . PHP_EOL; echo "data: $msg" . PHP_EOL; echo PHP_EOL; ob_flush(); flush(); } $serverTime = time(); sendMsg($serverTime, 'server time: ' . date("h:i:s", time())); ?&gt; </code></pre> <p>and the JavaScript looks like this (run on body load):</p> <pre><code>function init() { var source; if (!!window.EventSource) { source = new EventSource('events.php'); source.addEventListener('message', function(e) { document.getElementById('output').innerHTML += e.data + '&lt;br /&gt;'; }, false); source.addEventListener('open', function(e) { document.getElementById('output').innerHTML += 'connection opened&lt;br /&gt;'; }, false); source.addEventListener('error', function(e) { document.getElementById('output').innerHTML += 'error&lt;br /&gt;'; }, false); } else { alert("Browser doesn't support Server-Sent Events"); } } </code></pre> <p>I have searched around a bit but can't find information on</p> <ol> <li>If Apache needs any special configuration to support server-sent events, and </li> <li>How I can initiate a push from the server with this kind of setup (e.g. can I simply execute the PHP script from CLI to give a push to the already-connected-browser?)</li> </ol> <p>If I run this JS in Chrome (16.0.912.77) it opens the connection, receives the time, then errors (with no useful information in the error object), then reconnects in 3 seconds and goes through the same process. In Firefox (10.0) I get the same behaviour.</p> <p><strong>EDIT 1</strong>: I thought the issue could be related to the server I was using, so I tested on a vanilla XAMPP install and the same error comes up. Should a basic server configuration be able to handle this without modification / extra configuration?</p> <p><strong>EDIT 2</strong>: The following is an example of output from the browser:</p> <pre><code>connection opened server time: 01:47:20 error connection opened server time: 01:47:23 error connection opened server time: 01:47:26 error </code></pre> <p>Can anyone tell me where this is going wrong? The tutorials I have seen make it look like SSE is very straightforward. Also any answers to my two numbered questions above would be really helpful.</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload