Skip to content

Latest commit

 

History

History
53 lines (35 loc) · 1.02 KB

README.md

File metadata and controls

53 lines (35 loc) · 1.02 KB

Server-Sent Events

A basic NodeJS implementation of the Server-Sent Events specification.

Usage

Server

See ./routes/index.js for more in-depth server code (using Express). Including submitting custom events.

	var SSE = require('server-sent-events');

	// the route handler method (Express route)
	var routeHandler = function(request, response) {
		
		var sse = new SSE({
			response: response
		});

		sse.send('Hello, world.');

		setTimeout(function(){
			sse.send(100);
		}, 5000);

		sse.send({
			foo: 'bar'
		});
	}

Client

See ./public/javascripts/main.js for more in-depth client code. Including handling custom events.

	var route	= '/stream' // the route that the SSE is being sent on
		source	= new EventSource(route);


	source.on('message', function(){
		console.log(arguments);
	});

Running the built-in demo Express application

npm install && npm start

The server should then be available at http://localhost:3000