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

eventhub-jsclient

v2.4.0

Published

Eventhub.js is a JavaScript client library for Eventhub. It enables you to easily subscribe and publish to any eventhub server from the browser or Node.js.

Downloads

1,141

Readme

eventhub-jsclient

CI

eventhub-jsclient is a JavaScript client library for Eventhub. It enables you to easily subscribe and publish to a eventhub server from the browser or Node.js.

Installation

$ npm i --save eventhub-jsclient

Or, if you're oldschool:

$ wget -a scripts/eventhub-jsclient.js https://unpkg.com/eventhub-jsclient/dist/eventhub.umd.js

Or as a module directly from unpkg:

<script src="https://unpkg.com/eventhub-jsclient/dist/eventhub.umd.js" defer></script>
<!-- or -->
<script type="module">
    import Eventhub from 'https://unpkg.com/eventhub-jsclient/dist/eventhub.modern.js?module';
    const evClient = new Eventhub("ws://myeventhubserver.com", "myAuthToken");
</script>

Examples

Look in the examples directory for more examples.

Subscribe to a topic

const Eventhub = require('eventhub-jsclient');
const evClient = new Eventhub("ws://myeventhubserver.com", "myAuthToken");

evClient.connect().then(res => {
	evClient.subscribe("my/topic", function (msg) {
		console.log(`Topic: ${msg.topic}: Message ID: ${msg.id} Message: ${msg.message}`);
	});
}).catch(err => {
	console.log(`Error connecting to Eventhub: ${err}`);
});

Subscribe to a topic and get all historical (cached) events since a given point in time

const Eventhub = require('eventhub-jsclient');
const evClient = new Eventhub("ws://myeventhubserver.com", "myAuthToken");

evClient.connect().then(res => {
	evClient.subscribe("my/topic", function (msg) {
		console.log(`Topic: ${msg.topic}: Message ID: ${msg.id} Message: ${msg.message}`);
	}, {
		since: 1572811274719, // Return all cached events since timestamp specified in milliseconds.
		limit: 100 // Limit the amount of returned historical events to 100.
	);
}).catch(err => {
	console.log(`Error connecting to Eventhub: ${err}`);
});

You can also get all cached events since a given event id using the sinceEventId: <eventid> option instead of since: <timestamp>.

Publish to a topic

const Eventhub = require('eventhub-jsclient');
const evClient = new Eventhub("ws://myeventhubserver.com", "myAuthToken");

evClient.connect().then(res => {
	evClient.publish("my/topic", "This is a test message!", {
		ttl: 3600, // This message expires from the cache after 1 hour.
		timestamp: new Date().getTime() // Timestamp to index message with. If not set receipt time will be used.
	});
}).catch(err => {
	console.log(`Error connecting to Eventhub: ${err}`);
});

Unsubscribe from a topic

eventhub.unsubscribe("my/topic");

Unsubscribe from all subscribed topics

eventhub.unsubscribeAll();

Close connection to client

eventhub.disconnect();

List all current subscribed topics

eventhub.listSubscriptions().then(subscriptions => {
	console.log("Subscriptions:", subscriptions);
});

Get cached events for a topic without subscribing

// In this example we request all cached events from my/topic from the past 10 seconds.
// A negative 'since' number means Date.now() - abs(x).
// You can also speciy 'since' as a literal unix timestamp in milliseconds.
// We also support to request all events since a given message id by
// specifying 'sinceEventId': <id> instead of 'since'.
evClient.getEventlog("my/topic", { since: -10000 }).then((cache) => {
	for (const item of cache.items) {
		console.log(item);
	}
});

Reconnection handling

If the client loses connection with the server it will try to reconnect. When the connection is eventually restored all messages that has been lost during the disconnected period will be sent to the client before new ones.

Some of this behaviour is configureable as the third parameter to the connect() method.

Default options:

{
  pingInterval: 10000,      // Ping the server each 10 seconds.
  pingTimeout: 3000,        // Consider a ping as failed after 3 seconds.
  maxFailedPings: 3,        // How many lost pings before trying to reconnect.
  reconnectInterval: 10000, // 10 seconds between each reconnect attempt.
  disablePingCheck: false   // Disable pings and only rely on WebSocket 'onerror' event for detecting lost connection.
}

Lifecycle events

Library provides lifecycle events. You can subscribe for these events with the below syntax

const evClient = new Eventhub("ws://myeventhubserver.com", "myAuthToken");

evClient.on('connect', callback)

Event 'connect'

Emitted on successful (re)connection.

Event 'reconnect'

Emitted when a reconnect starts.

Event 'disconnect'

Emitted after a connection is being close.

Event 'offline'

Emitted when the client goes offline.

License

eventhub-jsclient is licensed under MIT. See LICENSE.