You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.2 KiB

4 years ago
  1. # EventSource [![npm version](http://img.shields.io/npm/v/eventsource.svg?style=flat-square)](http://browsenpm.org/package/eventsource)[![Build Status](http://img.shields.io/travis/EventSource/eventsource/master.svg?style=flat-square)](https://travis-ci.org/EventSource/eventsource)[![NPM Downloads](https://img.shields.io/npm/dm/eventsource.svg?style=flat-square)](http://npm-stat.com/charts.html?package=eventsource&from=2015-09-01)[![Dependencies](https://img.shields.io/david/EventSource/eventsource.svg?style=flat-square)](https://david-dm.org/EventSource/eventsource)
  2. This library is a pure JavaScript implementation of the [EventSource](https://html.spec.whatwg.org/multipage/server-sent-events.html#server-sent-events) client. The API aims to be W3C compatible.
  3. You can use it with Node.js or as a browser polyfill for
  4. [browsers that don't have native `EventSource` support](http://caniuse.com/#feat=eventsource).
  5. ## Install
  6. npm install eventsource
  7. ## Example
  8. npm install
  9. node ./example/sse-server.js
  10. node ./example/sse-client.js # Node.js client
  11. open http://localhost:8080 # Browser client - both native and polyfill
  12. curl http://localhost:8080/sse # Enjoy the simplicity of SSE
  13. ## Browser Polyfill
  14. Just add `example/eventsource-polyfill.js` file to your web page:
  15. ```html
  16. <script src=/eventsource-polyfill.js></script>
  17. ```
  18. Now you will have two global constructors:
  19. ```javascript
  20. window.EventSourcePolyfill
  21. window.EventSource // Unchanged if browser has defined it. Otherwise, same as window.EventSourcePolyfill
  22. ```
  23. If you're using [webpack](https://webpack.github.io/) or [browserify](http://browserify.org/)
  24. you can of course build your own. (The `example/eventsource-polyfill.js` is built with webpack).
  25. ## Extensions to the W3C API
  26. ### Setting HTTP request headers
  27. You can define custom HTTP headers for the initial HTTP request. This can be useful for e.g. sending cookies
  28. or to specify an initial `Last-Event-ID` value.
  29. HTTP headers are defined by assigning a `headers` attribute to the optional `eventSourceInitDict` argument:
  30. ```javascript
  31. var eventSourceInitDict = {headers: {'Cookie': 'test=test'}};
  32. var es = new EventSource(url, eventSourceInitDict);
  33. ```
  34. ### Allow unauthorized HTTPS requests
  35. By default, https requests that cannot be authorized will cause the connection to fail and an exception
  36. to be emitted. You can override this behaviour, along with other https options:
  37. ```javascript
  38. var eventSourceInitDict = {https: {rejectUnauthorized: false}};
  39. var es = new EventSource(url, eventSourceInitDict);
  40. ```
  41. Note that for Node.js < v0.10.x this option has no effect - unauthorized HTTPS requests are *always* allowed.
  42. ### HTTP status code on error events
  43. Unauthorized and redirect error status codes (for example 401, 403, 301, 307) are available in the `status` property in the error event.
  44. ```javascript
  45. es.onerror = function (err) {
  46. if (err) {
  47. if (err.status === 401 || err.status === 403) {
  48. console.log('not authorized');
  49. }
  50. }
  51. };
  52. ```
  53. ### HTTP/HTTPS proxy
  54. You can define a `proxy` option for the HTTP request to be used. This is typically useful if you are behind a corporate firewall.
  55. ```javascript
  56. var es = new EventSource(url, {proxy: 'http://your.proxy.com'});
  57. ```
  58. ## License
  59. MIT-licensed. See LICENSE