W3C compliant EventSource client for Node.js and browser (polyfill)
You can use it with Node.js or as a browser polyfill for
browsers that don't have native
npm install eventsource
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)
example/eventsource-polyfill.js file to your web page:
Now you will have two global constructors:
windowEventSourcePolyfillwindowEventSource // Unchanged if browser has defined it. Otherwise, same as window.EventSourcePolyfill
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
HTTP headers are defined by assigning a
headers attribute to the optional
var eventSourceInitDict = headers: 'Cookie': 'test=test';var es = url eventSourceInitDict;
By default, https requests that cannot be authorized will cause connection to fail and an exception to be emitted. You can override this behaviour:
var eventSourceInitDict = rejectUnauthorized: false;var es = url eventSourceInitDict;
Note that for Node.js < v0.10.x this option has no effect - unauthorized HTTPS requests are always allowed.
Unauthorized and redirect error status codes (for example 401, 403, 301, 307) are available in the
status property in the error event.
if errif errstatus === 401 || errstatus === 403console.log'not authorized';;
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 = url proxy: '' ;