846a29b5d34f9cb585465de89c59e9604bad4f638f271d92f45b6f2c4fbff8813cc0a8145b010aea38a8175c410dc5b41c0b122ae30e9805650f2b6b2349be 3.2 KB

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