Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

eventhubs-js

1.1.3 • Public • Published

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.

Install

npm i eventhubs-js

DownloadsWeekly Downloads

35

Version

1.1.3

License

Apache2

Last publish

Collaborators

  • avatar