Giter Club home page Giter Club logo

eventhubs-js's Introduction

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.

Simplest 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.

Other 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.

eventhubs-js's People

Contributors

ytechie avatar quy-le avatar denisbiondic avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.