title: JavaScript EventSource SSE not firing in browser
body: <p>I have been developing a nodejs server to provide server-side-events for a new website I am developing in HTML5.</p>
  
  <p>When I telnet to the server it works correctly, sending me the required HTTP response headers followed by a stream of events that i am presently generating every 2 or 3 seconds just to prove it works.</p>
  
  <p>I have tried the latest version of FireFox, Chrome and Opera and they create the EventSource object and connect to the nodejs server OK but none of the browsers generate any of the events, including the onopen, onmessage and onerror.</p>
  
  <p>However, if I stop my nodejs server, terminating the connection from the browsers, they all suddenly dispatch all the messages and all my events are shown. The browsers then all try to reconnect to the server as per spec.</p>
  
  <p>I am hosting everything on a webserver. nothing is running in local files.</p>
  
  <p>I have read everything I can find online, including books I've purchased and nothing indicates any such problem. Is there something Im missing?</p>
  
  <p><strong>A sample server implementation</strong></p>
  
  <pre><code>  var http = require('http');
    var requests = [];
  
    var server = http.Server(function(req, res) {
      var clientIP = req.socket.remoteAddress;
      var clientPort = req.socket.remotePort;
  
      res.on('close', function() {
        console.log("client " + clientIP + ":" + clientPort + " died");
  
        for(var i=requests.length -1; i&gt;=0; i--) {
          if ((requests[i].ip == clientIP) &amp;&amp; (requests[i].port == clientPort)) {
            requests.splice(i, 1);
          }
        }
      });
  
      res.writeHead(200, {
        'Content-Type': 'text/event-stream', 
        'Access-Control-Allow-Origin': '*', 
        'Cache-Control': 'no-cache', 
        'Connection': 'keep-alive'});
  
      requests.push({ip:clientIP, port:clientPort, res:res});
  
      res.write(": connected.\n\n");
    });
  
    server.listen(8080);
  
    setInterval(function test() {
      broadcast('poll', "test message");
    }, 2000);
  
  function broadcast(rtype, msg) {
    var lines = msg.split("\n");
  
    for(var i=requests.length -1; i&gt;=0; i--) {
      requests[i].res.write("event: " + rtype + "\n");
      for(var j=0; j&lt;lines.length; j++) {
        if (lines[j]) {
          requests[i].res.write("data: " + lines[j] + "\n");
        }
      }
      requests[i].res.write("\n");
    }
  }
  </code></pre>
  
  <p><strong>A sample html page</strong></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;SSE Test&lt;/title&gt;
      &lt;meta charset="utf-8" /&gt;
      &lt;script language="JavaScript"&gt;
        function init() {
          if(typeof(EventSource)!=="undefined") {
            var log = document.getElementById('log');
            if (log) {
              log.innerHTML = "EventSource() testing begins..&lt;br&gt;";
            }
  
            var svrEvents = new EventSource('/sse');
  
            svrEvents.onopen = function() {
              connectionOpen(true);
            }
  
            svrEvents.onerror = function() {
              connectionOpen(false);
            }
  
            svrEvents.addEventListener('poll', displayPoll, false);             // display multi choice and send back answer
  
            svrEvents.onmessage = function(event) {
                var log = document.getElementById('log');
                if (log) {
                  log.innerHTML += 'message: ' + event.data + "&lt;br&gt;";
                }
              // absorb any other messages
            }
          } else {
            var log = document.getElementById('log');
            if (log) {
              log.innerHTML = "EventSource() not supported&lt;br&gt;";
            }
          }
        }
  
        function connectionOpen(status) {
            var log = document.getElementById('log');
            if (log) {
              log.innerHTML += 'connected: ' + status + "&lt;br&gt;";
            }
        }
  
        function displayPoll(event) {
          var html = event.data;
            var log = document.getElementById('log');
            if (log) {
              log.innerHTML += 'poll: ' + html + "&lt;br&gt;";
            }
        }
      &lt;/script&gt;
    &lt;/head&gt;
    &lt;body onLoad="init()"&gt;
      &lt;div id="log"&gt;testing...&lt;/div&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>These examples are basic but of the same variety as every other demo i've seen in books and online. The eventSource only seems to be working if I end a client connection or terminate the server but this would be polling instead of SSE and I particularly want to use SSE.</p>
  
  <p>Interestingly, demos, such as thouse from <a href="http://www.html5rocks.com/en/tutorials/eventsource/basics/">html5rock</a> also seem to not quite work as expected when I use them online..</p>