
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
reconnecting-eventsource
Advanced tools
wrapper library around the JavaScript EventSource API to ensure it maintains a connection to the server.
This is a small wrapper library around the JavaScript EventSource API to ensure it maintains a connection to the server. Normally, EventSource
will reconnect on its own, however there are some cases where it may not. This library ensures a reconnect always happens.
To use it, just replace:
var es = new EventSource(url);
with:
var es = new ReconnectingEventSource(url);
npm install reconnecting-eventsource
In a browser environment, bring this in using:
<script src="/node_modules/reconnecting-eventsource/dist/ReconnectingEventSource.min.js"></script>
(You are free to copy this file out of node_modules
if you wish)
For node/browserify/webpack/etc, use:
import ReconnectingEventSource from "reconnecting-eventsource";
Note: This project assumes you have a working EventSource
available. If you are targeting a browser that doesn't support it, such as IE or Edge, you'll need to use a polyfill for EventSource.
Like the EventSource
, the constructor takes an optional configuration object: new ReconnectingEventSource(url, configuration)
. The configuration object is passed through to the underlying EventSource
and can optionally include the following configuration:
{
// indicating if CORS should be set to include credentials, default `false`
withCredentials: false,
// the maximum time to wait before attempting to reconnect in ms, default `3000`
// note: wait time is randomised to prevent all clients from attempting to reconnect simultaneously
max_retry_time: 3000,
// underlying EventSource class, default `EventSource`
eventSourceClass: EventSource,
}
If you wish to build this project, check out this repository and modify the source files in src/
. Then, run the following command:
npm run build
The resulting files are in lib/
for the node build and dist/
for the browser build.
EventSource
not reconnect?Typically, the normal EventSource
only reconnects if it is unable to reach the server at all. However, if the server is reached and it responds with an error (e.g. 500 status), then EventSource
will stop reconnecting.
Some errors, such as 502, are probably temporary, and ideally wouldn't cause client apps to break and require user intervention to get them started again.
The client has to explicitly stop by calling es.close()
. If you want to control this from the server, have the server send some kind of close instruction for the client to act on.
Browsers
Server
[1.6.4] - 2025-02-17
FAQs
wrapper library around the JavaScript EventSource API to ensure it maintains a connection to the server.
The npm package reconnecting-eventsource receives a total of 13,583 weekly downloads. As such, reconnecting-eventsource popularity was classified as popular.
We found that reconnecting-eventsource demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 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.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.