Html5 rocks node js server-sent-events SSE example not working

article link: http://www.html5rocks.com/en/tutorials/eventsource/basics/

In this example, the SSE node.js server is down. I get an open connection to /events , but the browser does not receive a response.

Chrome dev-tool prnt scrn

SSE-server.js

 var http = require('http'); var sys = require('sys'); var fs = require('fs'); http.createServer(function(req, res) { //debugHeaders(req); if (req.headers.accept && req.headers.accept == 'text/event-stream') { if (req.url == '/events') { sendSSE(req, res); } else { res.writeHead(404); res.end(); } } else { res.writeHead(200, {'Content-Type': 'text/html'}); res.write(fs.readFileSync(__dirname + '/sse-node.html')); res.end(); } }).listen(8000); function sendSSE(req, res) { res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive' }); var id = (new Date()).toLocaleTimeString(); // Sends a SSE every 5 seconds on a single connection. setInterval(function() { constructSSE(res, id, (new Date()).toLocaleTimeString()); }, 5000); constructSSE(res, id, (new Date()).toLocaleTimeString()); } function constructSSE(res, id, data) { res.write('id: ' + id + '\n'); res.write("data: " + data + '\n\n'); } function debugHeaders(req) { sys.puts('URL: ' + req.url); for (var key in req.headers) { sys.puts(key + ': ' + req.headers[key]); } sys.puts('\n\n'); } 

sse- node.html

 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> </head> <body> <script> var source = new EventSource('/events'); source.onmessage = function(e) { document.body.innerHTML += e.data + '<br>'; }; </script> </body> </html> 
+6
source share
5 answers

The problem was in the server. In my case, I used node with IIS using the iisnode node package. To solve this problem, I needed to configure iisnode in the web.config file as follows:

 <iisnode flushResponse="true" /> 

After that, everything worked fine. Others with a similar problem may start here, but apache and nginx may have similar configuration requirements.

+2
source

Why did you compile res.end () at the end of the sendSSE function? This is a method that actually sends a response to the browser.

+1
source

I am already trying to use this code and working for me since you are not using ngnix or any other server as a proxy server for your node instances. I believe the problem is with your machine if you have a firewall or antivirus to start the virus, stop it and try again or any other software that can intercept your req and res.

+1
source

Make sure you save the HTML file with the same name as the one described by sse-node.html in the same directory. Another thing is to make sure that port 8000 is open on your local computer and no one is using it. or change the port and run sse-server.js. his work is for me as it is.

0
source

Specify the full resource

 var source = new EventSource('http://localhost:8000/events'); //true way 

instead

 var source = new EventSource('http://localhost:8000/events'); //improper way 
0
source

All Articles