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

eventhubs-js

v1.1.3

Published

Easy to use client for Azure event hubs

Downloads

3,609

Readme

An Azure Event Hub client that is easy to use and performs well. From a local machine, I'm able to sustain ~300 single messages per second from a single client. When running in an Azure VM in the same region as the Event Hubs instance, I was able to send ~400 single messages per second. From a Raspberry PI, I was able to send ~40 single messages per second. There is an option for batching messages if needed by using the sendMessages function.

Easy Dev Usage

eventHubs.init({
    hubNamespace: eventHubsNamespace,
    hubName: eventHubsHubName,
    keyName: eventHubsKeyName,
    key: eventHubsKey
});

var deviceMessage = {
    Temperature: 45.2,
    Pressure: 23.7
}

eventHubs.sendMessage({
    message: deviceMessage,
    deviceId: 1,
});

Note: deviceId is simply a unique name to identify your device to Azure. If not given, you will recieve a 401 Authorization failed response.

Production Usages

When you initialize the event hubs client, it's advisable to use a SAS token in a production environment. This is a revokable key that is unique to the device. You can generate a token programatically, or online using this form.

eventHubs.init({
    hubNamespace: eventHubsNamespace,
    hubName: eventHubsHubName,
    sasToken: sasToken
});

For batching multiple messages and sending them together in single REST call, use the sendMessages function:

eventHubs.sendMessages({
    messages: [], // array of messages
    deviceId: 1,
});

Beware that Azure EventHub REST API has a limit of 256kb for batching (per call), so make sure you batched messages do not reach this limit (otherwise an error will be returned).

Installation

npm install eventhubs-js

Don't forget to update your package.json file.

Performance Optimizations

Performance was optimized in a number of ways:

  1. Setting http.globalAgent.maxSockets = 50; increases the HTTP connection pool, which allows us to create more connections to serve messages that need sent. If you don't send a large volume of messages, no problem, the pool will remain relatively empty.
  2. Caching the SAS Tokens. I haven't tested the performance of the node.js crypto libraries and moment time calculations, but it was easy enough to cache the generated SAS tokens to avoid recalcuating on each message.
  3. Automatic recreation of cached token just before it expires

Examples & Promises

Promises allow you to chain calls without "callback hell":

eventHubs.sendMessage({
    message: deviceMessage,
    deviceId: 1,
}).then(function() {
console.log('Message Sent!');
});

Promises also allow us to kick of multiple send requests simultaneously, and easily manage the results:

var promise, promises;

for (i = 0; i < iterations; i++) {
    promise = eventHubs.sendMessage({
    	message: deviceMessage,
		deviceId: 1,
	});

    promises.push(promise);
}
Q.allSettled(promises).then(function () {
    console.log('All Messages Sent!');
});

Alternative Clients

License

Microsoft Developer Experience & Evangelism

Copyright (c) Microsoft Corporation. All rights reserved.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.

The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, email address, logo, person, places, or events is intended or should be inferred.