
Security News
Axios Supply Chain Attack Reaches OpenAI macOS Signing Pipeline, Forces Certificate Rotation
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.
eventhub-jsclient
Advanced tools
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.
eventhub-jsclient is a JavaScript client library for Eventhub. It enables you to easily subscribe and publish to an Eventhub server from the browser, Node.js and Deno.
$ npm i --save eventhub-jsclient
Or, if you're old-school:
$ 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"></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>
Look in the examples directory for more examples.
Subscribe to a topic
import Eventhub from 'eventhub-jsclient';
const evClient = new Eventhub('ws://myeventhubserver.com', 'myAuthToken');
await evClient.connect();
evClient.subscribe('my/topic', (msg) => {
const { topic, id, message } = msg;
console.log(`Topic: ${topic} Message ID: ${id} Message: ${message}`);
// message is _always_ string, you need to parse it with JSON.parse if you sent JSON.
const data = JSON.parse(message);
console.log('Parsed data:', data);
});
Subscribe to a topic and get all historical (cached) events since a given point in time
evClient.subscribe('my/topic', messageHandler, {
since: 1572811274719, // Return all cached events since timestamp specified in milliseconds.
limit: 100, // Limit the amount of returned historical events to 100.
});
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
import Eventhub from 'eventhub-jsclient';
const evClient = new Eventhub('ws://myeventhubserver.com', 'myAuthToken');
await evClient.connect();
await evClient.publish('my/topic', 'This is a test message!', {
ttl: 3600, // TTL in seconds. This message expires from the cache after 1 hour.
timestamp: Date.now(), // Timestamp to index message with. If not set receipt time will be used.
});
Unsubscribe from a topic
eventhub.unsubscribe('my/topic');
Unsubscribe from all subscribed topics
eventhub.unsubscribeAll();
Close connection to client
await eventhub.disconnect();
List all current subscribed topics
const subscriptions = await eventhub.listSubscriptions();
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'.
const cache = await evClient.getEventlog('my/topic', { since: -10000 });
for (const item of cache.items) {
console.log(item);
}
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 behavior is configurable 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.
}
Library provides life-cycle events. You can subscribe for these events with the below syntax
evClient.on('connect', callback);
'connect'Emitted on successful (re)connection.
'reconnect'Emitted when a reconnect starts.
'disconnect'Emitted after a connection is being close.
'offline'Emitted when the client goes offline.
eventhub-jsclient is licensed under MIT. See LICENSE.
FAQs
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.
We found that eventhub-jsclient demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
OpenAI rotated macOS signing certificates after a malicious Axios package reached its CI pipeline in a broader software supply chain attack.

Security News
Open source is under attack because of how much value it creates. It has been the foundation of every major software innovation for the last three decades. This is not the time to walk away from it.

Security News
Socket CEO Feross Aboukhadijeh breaks down how North Korea hijacked Axios and what it means for the future of software supply chain security.