Skip to content

Instantly share code, notes, and snippets.

@molnarg
Created October 18, 2011 12:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save molnarg/1295296 to your computer and use it in GitHub Desktop.
Save molnarg/1295296 to your computer and use it in GitHub Desktop.
Node.js server push example
var http = require('http');
var listener;
http.createServer(function (request, response) {
if (request.url === '/listen') {
listener = response;
return;
}
if (request.url.indexOf('/event') === 0 && listener !== undefined) {
listener.writeHead(200, {'Content-Type': 'text/plain'});
listener.write('Event: ' + request.url.slice(7))
listener.end();
listener = undefined;
}
response.end()
}).listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment