Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
server sent event test
var SSE = require('sse')
, http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('<html><head><title></title></head><body>');
res.write('<script src="https://raw.githubusercontent.com/Yaffle/EventSource/master/eventsource.js"></script><script>');
res.write('var es = new EventSource("/sse");es.onmessage = function (event) {alert(event.data);};');
res.write('</script></body></html>');
res.end();
});
var count = 0;
server.listen(8765, function() {
var sse = new SSE(server);
sse.on('connection', function(client) {
setInterval(function () {
count++;
console.log('send', count);
client.send(count + Array(10000).join(',') + ';');
setTimeout(function () {
client.send(count + Array(5000).join(',') + ';');
}, 1000);
}, 10000);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment