Search code examples
javascriptnode.jshttpevent-stream

nodejs server side events just don't want to work


I have been trying to figure this out for hours...

I used various tutorials that explained it, copied the code into my code, but it just doesn't work. No error message, nothing.

Here is what i'm doing:

client:

<script>
startStream()
function startStream()
{
    source = new EventSource("/stream")
    if(typeof(EventSource) !== "undefined") {
        console.log("streams are supported")

        source.addEventListener('message', function(e) {
          console.log(e.data);
        }, false);

        source.onerror = function(event) {
            source.close();
        }
    } else {
        console.log("no stream support")
    }
}
</script>

server:

http = require('http')
fs = require('fs')

var server = http.createServer(function(request, response)
{
    //understand the request
    var path = request.url
    if(path == "/")
        path = "/index.html"
    console.log("request for "+path)
    if (path.indexOf("stream") != -1)
    {
        console.log("- client requesting stream")
        response.writeHead(200, {"Content-Type":"text/event-stream", "Cache-Control":"no-cache", "Connection":"keep-alive"})
        response.write('\n')

        var interval = setInterval(function() {
            console.log("- sending stream data")
            response.write("data: message")
            response.write('\n')
        }, 1000);

        request.connection.addListener("close", function () {
              clearInterval(interval);
        }, false);
        return 0
    }

    var html = fs.readFileSync("index.html","utf-8").toString();
    response.writeHead(200, {"Content-Type": "text/html"})
    response.write(html)
    response.end()
})

//wait for requests
var port = 5001
server.listen(port)
console.log("listening on port "+port+"...")

here on stackoverflow someone also had problems with streams, it was then suggested that response.end() has to be inserted. It worked for that person. I just get an error that i can't write after the connection is closed when i do this. It's like magic ("Error: write after end")

server output:

request for /stream
- client requesting stream
- sending stream data
- sending stream data
- sending stream data
- sending stream data
- sending stream data

client output:

streams are supported

client response headers:

HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
Connection: keep-alive
Date: Sun, 23 Apr 2017 09:48:04 GMT
Transfer-Encoding: chunked

client request headers:

GET /stream HTTP/1.1
Host: localhost:5000
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0
Accept: text/event-stream
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:5000/
Cookie: 0f62446157da624a2edb8a2b53d86dc1=de-DE
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

there is nothing more going on in the network inspector besides this


Solution

  • good god finally, shortly after posting the question

    each message has to contain TWO \n at the end

    response.write("data: message")
    response.write('\n\n')