Skip to content

Instantly share code, notes, and snippets.

@milingo
Created June 15, 2017 14:49
Show Gist options
  • Save milingo/4e81ca987a0ac5b07e8023b8f4462c28 to your computer and use it in GitHub Desktop.
Save milingo/4e81ca987a0ac5b07e8023b8f4462c28 to your computer and use it in GitHub Desktop.
const http = require('http');
const server = http.createServer(function (req, res) {
if (req.url === '/live') {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
res.write('retry: 5000\n');
const interval = setInterval(() => {
res.write('data: ' + new Date() + '\n\n');
}, 1000);
req.on('end', () => clearInterval(interval));
return;
}
// Normal requests
return res.end();
});
server.listen(3000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment