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

eventsource-monitor

v0.9.4

Published

Monitors and manages EventSource, which amounts to making the API a little easier for my case and adding the ability to switch URLs

Downloads

15

Readme

eventsource-monitor

Monitors and manages up to two EventSource, to give you the ability to smoothly change URL's, still recieving messages from the first while the second is coming online while also giving you many useful events which fire when things change (disconnections, url change complete etc).

Usage

First you will need to define a factory method which will be used for creating EventSource instances. The input for this function can be anything you wish:

var eventSourceFactory = function(url) {
	return new EventSource(url);
};

Now we have our EventSource factory we can use it to create our EventSourceMonitor:

var eventSourceMonitor = new EventSourceMonitor(
	eventSourceFactory,
	'http://yourdomain.com/eventsource?datasets[]=ds21&datasets[]=46'
);

EventSource has a good few events which it can fire to tell you what is happening with your connection(s). Two of the most helpful are the "connected" and "disconnected":

eventSourceMonitor.on('connected', function(evt) {
	console.log('Uh Oh, you seem to be disconnected...');
});
eventSourceMonitor.on('connected', function(evt) {
	console.log('I am connected to ' + evt.url);
});

eventSourceMonitor.connect();

Of course you need to get the messages which are sent to you by the EventSource:

eventSourceMonitor.on('messaged', function(data) {
	console.log(
		"The following data was messaged: " +
		JSON.stringify(data)
	);
});

Where EventSourceMonitor monitor becomes useful that it will manage changing of the URL you are connected to, note that EventSourceMonitor will continue to route events from the existing connection until the new connection is established.

eventSourceMonitor.on('url-changed-was-online', function(evt) {
	console.log("The URL we are listening to events on has been changed to " + evt.url);
});
eventSourceMonitor.on('url-changed', function(evt) {
	console.log("The 'url-changed' event will also be fired");
});
eventSourceMonitor.changeUrl(
	'http://yourdomain.com/eventsource?datasets[]=ds21&datasets[]=46&datasets[]=94'
);

Either by request, or because of the internet / server you can become disconnected, in which case you will be notified of it:

eventSourceMonitor.on('disconnected', function(evt) {
	console.log("You have been disconnected");
});
eventSourceMonitor.disconnect();

You can also change urls when you are disconnected:

eventSourceMonitor.on('url-changed-was-offline', function(evt) {
	console.log(
		"Even though you are offline the URL we are listening to " +
		"events on has been changed to '" + evt.url + "
	);
});
eventSourceMonitor.on('url-changed', function(evt) {
	console.log("The 'url-changed' event will always be fired, even if offline");
});
eventSourceMonitor.changeUrl(
	'http://yourdomain.com/eventsource?datasets[]=ds21&datasets[]=46&datasets[]=94'
);

If you want to reconnect!

eventSourceMonitor.connect();

Other Events

The following extra events are exposed:

  • added-managed-connection: An EventSource has been added, which is probably not connected.
  • removed-managed-connection: An EventSource has been removed due to url changes.
  • url-change-started: We have started to change the URL we are connected to.

Source Code

Source code is prepared using Browserify which is also compatible with Node.JS. There is a UMD bundle which can be used with AMD or a vanilla browser (where it will export a global called called EventsourceMonitor.