This library is a pure Java\+Script implementation of the \href{https://html.spec.whatwg.org/multipage/server-sent-events.html\#server-sent-events}{\texttt{ Event\+Source}} client. The API aims to be W3C compatible.

You can use it with Node.\+js or as a browser polyfill for \href{http://caniuse.com/\#feat=eventsource}{\texttt{ browsers that don\textquotesingle{}t have native {\ttfamily Event\+Source} support}}.\hypertarget{md_sfcode_node_modules_eventsource_README_autotoc_md9346}{}\doxysection{Install}\label{md_sfcode_node_modules_eventsource_README_autotoc_md9346}
\begin{DoxyVerb}npm install eventsource
\end{DoxyVerb}
 \hypertarget{md_sfcode_node_modules_eventsource_README_autotoc_md9347}{}\doxysection{Example}\label{md_sfcode_node_modules_eventsource_README_autotoc_md9347}
\begin{DoxyVerb}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
\end{DoxyVerb}
 \hypertarget{md_sfcode_node_modules_eventsource_README_autotoc_md9348}{}\doxysection{Browser Polyfill}\label{md_sfcode_node_modules_eventsource_README_autotoc_md9348}
Just add {\ttfamily example/eventsource-\/polyfill.\+js} file to your web page\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{<script src=/eventsource-\/polyfill.js></script> }

\end{DoxyCode}


Now you will have two global constructors\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{window.EventSourcePolyfill}
\DoxyCodeLine{window.EventSource // Unchanged if browser has defined it. Otherwise, same as window.EventSourcePolyfill }

\end{DoxyCode}


If you\textquotesingle{}re using \href{https://webpack.github.io/}{\texttt{ webpack}} or \href{http://browserify.org/}{\texttt{ browserify}} you can of course build your own. (The {\ttfamily example/eventsource-\/polyfill.\+js} is built with webpack).\hypertarget{md_sfcode_node_modules_eventsource_README_autotoc_md9349}{}\doxysection{Extensions to the W3\+C API}\label{md_sfcode_node_modules_eventsource_README_autotoc_md9349}
\hypertarget{md_sfcode_node_modules_eventsource_README_autotoc_md9350}{}\doxysubsection{Setting HTTP request headers}\label{md_sfcode_node_modules_eventsource_README_autotoc_md9350}
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 {\ttfamily Last-\/\+Event-\/\+ID} value.

HTTP headers are defined by assigning a {\ttfamily headers} attribute to the optional {\ttfamily event\+Source\+Init\+Dict} argument\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var eventSourceInitDict = \{headers: \{'Cookie': 'test=test'\}\};}
\DoxyCodeLine{var es = new EventSource(url, eventSourceInitDict); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_eventsource_README_autotoc_md9351}{}\doxysubsection{Allow unauthorized HTTPS requests}\label{md_sfcode_node_modules_eventsource_README_autotoc_md9351}
By default, https requests that cannot be authorized will cause the connection to fail and an exception to be emitted. You can override this behaviour, along with other https options\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{var eventSourceInitDict = \{https: \{rejectUnauthorized: false\}\};}
\DoxyCodeLine{var es = new EventSource(url, eventSourceInitDict); }

\end{DoxyCode}


Note that for Node.\+js $<$ v0.\+10.\+x this option has no effect -\/ unauthorized HTTPS requests are {\itshape always} allowed.\hypertarget{md_sfcode_node_modules_eventsource_README_autotoc_md9352}{}\doxysubsection{HTTP status code on error events}\label{md_sfcode_node_modules_eventsource_README_autotoc_md9352}
Unauthorized and redirect error status codes (for example 401, 403, 301, 307) are available in the {\ttfamily status} property in the error event.


\begin{DoxyCode}{0}
\DoxyCodeLine{es.onerror = function (err) \{}
\DoxyCodeLine{  if (err) \{}
\DoxyCodeLine{    if (err.status === 401 || err.status === 403) \{}
\DoxyCodeLine{      console.log('not authorized');}
\DoxyCodeLine{    \}}
\DoxyCodeLine{  \}}
\DoxyCodeLine{\}; }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_eventsource_README_autotoc_md9353}{}\doxysubsection{HTTP/\+HTTPS proxy}\label{md_sfcode_node_modules_eventsource_README_autotoc_md9353}
You can define a {\ttfamily proxy} option for the HTTP request to be used. This is typically useful if you are behind a corporate firewall.


\begin{DoxyCode}{0}
\DoxyCodeLine{var es = new EventSource(url, \{proxy: 'http://your.proxy.com'\}); }

\end{DoxyCode}
\hypertarget{md_sfcode_node_modules_eventsource_README_autotoc_md9354}{}\doxysection{License}\label{md_sfcode_node_modules_eventsource_README_autotoc_md9354}
MIT-\/licensed. See LICENSE 