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