Join us!
Want to join the team?

We would love to get to know you

Are you looking for an employer who cares about you as a person and where you feel involved in everything that concerns you? Welcome to JellyHive!
Who are we? We are a successful IT consultant company and our philosophy is that the company is our employees.
Who are you? You are a system developer (fullstack, frontend, backend), and/or maybe also a scrum master, test lead, devops etc.
What we offer you Participation in developing the company with generous benefits.


Upload your cv Supported filetypes are: .pdf, .doc and .docx
Hide
Stefan Matsson 2017-05-08

Send messages with properties to Azure Event Hubs from Node

Azure Event Hubs is a “highly scalable data streaming platform capable of ingesting millions of events per second”. Basically it allows you to integrate different systems by sending asynchronous messages resulting in low coupling between systems.

For .NET just grab the nuget package and you’re up and running. For Node there is a NPM package called azure-event-hubs. It is maintained by Microsoft and is currently in version 0.0.7: https://www.npmjs.com/package/azure-event-hubs

Writing to and reading from event hubs is pretty straight forward. Examples can be found on Github: https://github.com/Azure/azure-event-hubs-node/blob/master/samples/javascript-eventhub-sample/simple_eventhubs_client.js

Sending messages containing properties becomes a bit more complicated. In .NET you would simply add your properties to the EventData.Properties collection and then send the message. In azure-event-hubs for Node, the option to add properties is not exposed. Fortunately the EventHubSender returned by EventHubClient.createSender exposes the underlying AMQP (Advanced Message Queuing Protocol)  link used. We can use this to send a raw AMQP message to event hubs.


var azure = require(‘azure-event-hubs’);
var eventHubsClient = azure.Client.fromConnectionString(‘connection string’, ‘event hub name’);

var event = “data to send”; // could be an object or whatever. No need to serialize it beforehand.

eventHubsClient.open()
.then(function() {
return eventHubsClient.createSender();
})
.then(function(tx) {
tx.on(‘errorReceived’, function (err) {
console.error(JSON.stringify(err));
});

<span class="cm-keyword">var</span> <span class="cm-def">options</span> <span class="cm-operator">=</span> {
    <span class="cm-property">applicationProperties</span>: {
        <span class="cm-string cm-property">'my property'</span>:<span class="cm-string">'some value'</span>
    }
};

<span class="cm-keyword">var</span> <span class="cm-def">msg</span> <span class="cm-operator">=</span> {
    <span class="cm-property">body</span>: <span class="cm-variable">event</span>
};

<span class="cm-keyword">return</span> <span class="cm-variable-2">tx</span>.<span class="cm-property">_senderLink</span>.<span class="cm-property">send</span>(<span class="cm-variable-2">msg</span>, <span class="cm-variable-2">options</span>);

})
.catch(function(err) {
console.error(err.message);
});

The properties placed in options -> applicationProperties will be added to the Properties collection in .NET. If you wish to add system properties such as EnqueuedTimeUtc or PartitionKey these should be added to options -> messageAnnotations. The system properties are named as below:






















C# AMQP
SequenceNumber x-opt-sequence-number
Offset x-opt-offset
EnqueuedTimeUtc x-opt-enqueued-time
PartitionKey x-opt-partition-key

Comments