UNPKG

3.26 kBMarkdownView Raw
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
5This 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
7You 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
24Just add `example/eventsource-polyfill.js` file to your web page:
25
26```html
27<script src=/eventsource-polyfill.js></script>
28```
29
30Now you will have two global constructors:
31
32```javascript
33window.EventSourcePolyfill
34window.EventSource // Unchanged if browser has defined it. Otherwise, same as window.EventSourcePolyfill
35```
36
37If you're using [webpack](https://webpack.github.io/) or [browserify](http://browserify.org/)
38you 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
44You can define custom HTTP headers for the initial HTTP request. This can be useful for e.g. sending cookies
45or to specify an initial `Last-Event-ID` value.
46
47HTTP headers are defined by assigning a `headers` attribute to the optional `eventSourceInitDict` argument:
48
49```javascript
50var eventSourceInitDict = {headers: {'Cookie': 'test=test'}};
51var es = new EventSource(url, eventSourceInitDict);
52```
53
54### Allow unauthorized HTTPS requests
55
56By default, https requests that cannot be authorized will cause the connection to fail and an exception
57to be emitted. You can override this behaviour, along with other https options:
58
59```javascript
60var eventSourceInitDict = {https: {rejectUnauthorized: false}};
61var es = new EventSource(url, eventSourceInitDict);
62```
63
64Note 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
68Unauthorized and redirect error status codes (for example 401, 403, 301, 307) are available in the `status` property in the error event.
69
70```javascript
71es.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
82You 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
85var es = new EventSource(url, {proxy: 'http://your.proxy.com'});
86```
87
88
89## License
90
91MIT-licensed. See LICENSE
92
\No newline at end of file