Server-Sent Events for Node.js
JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example
lib
.gitignore
README.md
package.json

README.md

Server Sent Events for Node.js

With built in channels

Spec: http://dev.w3.org/html5/eventsource/

Install

npm install nsse

Usage

var ServerSent = require('nsse')
   , http = require('http')
   , os = require('os')

var server = http.createServer(function(req, res){
    res.end('Server Sent Events')
})

server.listen(8100)


var ss = new ServerSent(server, /* { heartbeat: 5*1000 } */) // default for 10 secs

var channel = ss.of('/sse') // like socket.io

channel.on('connection', function (socket){
	socket.event('firstevnt')
	socket.end('end of event')

	socket.event('test')
	socket.write('this is a test')
	socket.json({
		serverDate: +new Date,
		loadavg: os.loadavg()
	})

	socket.send('yetAnotherEvent', 'data for this event')

	setInterval(function (){
		socket.json({
			serverDate: +new Date,
			loadavg: os.loadavg()
		})
	}, 5000)
})

Client:

<html>
	<body>
		<h1> Server Sent Events</h1>

		<script type="text/javascript">
		var source = new EventSource('/sse')

		source.addEventListener('firstevnt', function (ping){
			console.log('frst:', ping.data)
		})

		source.addEventListener('test', function (ev){
			console.log('DATA:', JSON.parse(ev.data))
		})

		source.addEventListener('heartbeat', function (ev){
			console.log('heartbeat ->', ev.data)
		})

		</script>
	</body>
</html>

Other libraries

For the browser:

-- jQuery EventSource by Rick Waldron

License

Alejandro Morales (c) MIT 2012