Skip to content

TravelingTechGuy/express-eventsource

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Server Side Events using Node, Express and EventSource

This simple demo shows streaming events from server to client, using EventSource.

  • The server (app.js) streams a list of 10 random numbers, one every 3 seconds, or until the client stops listening.
  • The client (main.js) listens to the stream, prints the results and stops listening after the last number has been received.

Install

You may need to install some missing modules with npm

Once node and npm are installed, you can checkout the source and install the missing modules:

$ git clone https://github.com/TravelingTechGuy/express-eventsource.git
$ cd ./express-eventsource
$ npm install
$ node app

Now, browse to your localhost (ie: http://localhost:3000) in your web browser.

Online demo

An online demo is available on Heroku at http://express-eventsource.herokuapp.com/ Online demo

Missing from the demo:

  • Proper error handling
  • Tests
  • Grunt project

IE Support

Since IE (at least up to version 9) does not support EventSource, I'm using this polyfill by Yaffle. I've used Modernizr to bootstrap it when needed (and found out I need to bootstrap the test into Modernizr :) ). It requires some server-side changes:

  1. The event name and the way it's added
  2. Response headers
  3. An additional 2k padding at the top of the header - apparently necessary

Ingredients

###Client side:

###Server side:

About

EventSource with Node and Express

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published