Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
@heroku/eventsource
Advanced tools
W3C compliant EventSource client for Node.js and browser (polyfill)
This library is a pure JavaScript implementation of the EventSource client. The API aims to be W3C compatible.
You can use it with Node.js or as a browser polyfill for
browsers that don't have native EventSource
support.
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
Just add example/eventsource-polyfill.js
file to your web page:
<script src=/eventsource-polyfill.js></script>
Now you will have two global constructors:
window.EventSourcePolyfill
window.EventSource // Unchanged if browser has defined it. Otherwise, same as window.EventSourcePolyfill
If you're using webpack or browserify
you can of course build your own. (The example/eventsource-polyfill.js
is built with webpack).
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 Last-Event-ID
value.
HTTP headers are defined by assigning a headers
attribute to the optional eventSourceInitDict
argument:
var eventSourceInitDict = {headers: {'Cookie': 'test=test'}};
var es = new EventSource(url, eventSourceInitDict);
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:
var eventSourceInitDict = {https: {rejectUnauthorized: false}};
var es = new EventSource(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.
es.onerror = function (err) {
if (err) {
if (err.status === 401 || err.status === 403) {
console.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 = new EventSource(url, {proxy: 'http://your.proxy.com'});
MIT-licensed. See LICENSE
FAQs
W3C compliant EventSource client for Node.js and browser (polyfill)
The npm package @heroku/eventsource receives a total of 5,546 weekly downloads. As such, @heroku/eventsource popularity was classified as popular.
We found that @heroku/eventsource demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 95 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.