No description
  • JavaScript 99.8%
  • HTML 0.2%
Find a file
Mathieu Champlon a950a229dc
Merge pull request #1 from rumpl/master
Only parse url if it's a string
2021-11-26 11:08:20 +01:00
.github Create dependabot.yml 2021-05-08 10:43:47 +02:00
example Support HTTP 302 redirects (#116) 2020-01-24 16:49:48 -08:00
lib Only parse url if it's a string 2021-11-24 14:32:50 +01:00
test Fix issue: reconnection only happends for 1 time after connection drops 2020-05-24 18:43:34 +08:00
.editorconfig Add editorconfig to match coding standard 2017-04-17 17:47:17 +02:00
.gitignore Add nyc code coverage reporting 2017-04-17 19:33:24 +02:00
.npmignore add .github/ folder to npmignore 2021-05-06 11:05:34 +02:00
CONTRIBUTING.md Update URLs to match new EventSource organization 2017-04-17 13:38:45 +02:00
HISTORY.md Update history for 1.1.0 (#146) 2021-03-17 21:11:17 -07:00
LICENSE Transfer copyright to the EventSource GitHub organisation 2017-01-17 23:15:09 +00:00
package-lock.json 1.1.0 2021-03-17 21:13:24 -07:00
package.json bump engines node to >= 12.0.0 2021-05-05 11:45:49 -07:00
README.md add GitHub Action build status badge to readme 2021-05-05 11:59:33 -07:00

EventSource npm versionNPM DownloadsDependencies

Build

This library is a pure JavaScript implementation of the EventSource client. The API aims to be W3C compatible.

You can use it with Node.js or as a browser polyfill for browsers that don't have native EventSource support.

Install

npm install eventsource

Example

npm install
node ./example/sse-server.js
node ./example/sse-client.js    # Node.js client
open http://localhost:8080      # Browser client - both native and polyfill
curl http://localhost:8080/sse  # Enjoy the simplicity of SSE

Browser Polyfill

Just add example/eventsource-polyfill.js file to your web page:

<script src=/eventsource-polyfill.js></script>

Now you will have two global constructors:

window.EventSourcePolyfill
window.EventSource // Unchanged if browser has defined it. Otherwise, same as window.EventSourcePolyfill

If you're using webpack or browserify you can of course build your own. (The example/eventsource-polyfill.js is built with webpack).

Extensions to the W3C API

Setting HTTP request headers

You can define custom HTTP headers for the initial HTTP request. This can be useful for e.g. sending cookies or to specify an initial Last-Event-ID value.

HTTP headers are defined by assigning a headers attribute to the optional eventSourceInitDict argument:

var eventSourceInitDict = {headers: {'Cookie': 'test=test'}};
var es = new EventSource(url, eventSourceInitDict);

Allow unauthorized HTTPS requests

By default, https requests that cannot be authorized will cause the connection to fail and an exception to be emitted. You can override this behaviour, along with other https options:

var eventSourceInitDict = {https: {rejectUnauthorized: false}};
var es = new EventSource(url, eventSourceInitDict);

Note that for Node.js < v0.10.x this option has no effect - unauthorized HTTPS requests are always allowed.

HTTP status code on error events

Unauthorized and redirect error status codes (for example 401, 403, 301, 307) are available in the status property in the error event.

es.onerror = function (err) {
  if (err) {
    if (err.status === 401 || err.status === 403) {
      console.log('not authorized');
    }
  }
};

HTTP/HTTPS proxy

You can define a proxy option for the HTTP request to be used. This is typically useful if you are behind a corporate firewall.

var es = new EventSource(url, {proxy: 'http://your.proxy.com'});

License

MIT-licensed. See LICENSE