npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

reconnecting-eventsource

v1.6.2

Published

wrapper library around the JavaScript EventSource API to ensure it maintains a connection to the server.

Downloads

24,040

Readme

ReconnectingEventSource

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);

Adding to your project

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.

Configuration

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,
}

Building from source

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.

When does the normal 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.

Why reconnect if the server responds with an error?

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.

So this thing reconnects forever. How do I make it stop?

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.