0.0.2 • Published 8 years ago

tim-azure-event-hubs v0.0.2

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

azure-event-hubs

npm version

Usage

This library is primarily promise-based (for now, using Bluebird). See tests/sender_test.js or tests/receiver_test.js for some basic examples.

The simplest usage is to instantiate the main EventHubClient class with a ConnectionConfig, or use the static factory method EventHubClient.fromConnectionString(_connection-string_, _event-hub-path_). Once you have a client, you can use it to create senders and/or receivers using the client.createSender or client.createReceiver methods. Receivers emit message events when new messages come in, while senders have a simple send method that allows you to easily send messages (with an optional partition key).

Example 1 - Get the partition IDs.

var EventHubClient = require('azure-event-hubs').Client;

var client = EventHubClient.fromConnectionString('Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key', 'myeventhub')
client.getPartitionIds().then(function(ids) {
    ids.forEach(function(id) { console.log(id); });
});

Example 2 - Create a receiver

Creates a receiver on partition ID 10, for messages that come in after "now".

var EventHubClient = require('azure-event-hubs').Client;

var client = EventHubClient.fromConnectionString('Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key', 'myeventhub')
client.createReceiver('$Default', '10', { startAfterTime: Date.now() })
    .then(function (rx) {
        rx.on('errorReceived', function (err) { console.log(err); }); 
        rx.on('message', function (message) {
            var body = message.body;
            // @see eventdata.js
            var enqueuedTime = Date.parse(message.enqueuedTimeUtc);
        });
    });

Example 3 - Create a sender v1

Creates a sender, sends to a given partition "key" which is then hashed to a partition ID (so all messages with the same key will go to the same ID, but load is balanced between partitions).

var EventHubClient = require('azure-event-hubs').Client;

var client = EventHubClient.fromConnectionString('Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key', 'myeventhub')
client.createSender()
    .then(function (tx) {
        tx.on('errorReceived', function (err) { console.log(err); });
        tx.send({ contents: 'Here is some text sent to partition key my-pk.' }, 'my-pk'); 
    });

Example 4 - Create a sender v2

Creates a sender against a given partition ID (10). You should use send to a given partition key, but if you need to send to a given partition ID we'll let you do it.

var EventHubClient = require('azure-event-hubs').Client;

var client = EventHubClient.fromConnectionString('Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key', 'myeventhub')
client.createSender('10')
    .then(function (tx) {
        tx.on('errorReceived', function (err) { console.log(err); });
        tx.send({ contents: 'Here is some text sent to partition 10.' }); 
    });

Example 5 - Create a receiver with a custom policy

Creates a receiver for a given partition ID, with a larger max message size than the default (10000 bytes).

var EventHubClient = require('azure-event-hubs').Client;

var client = EventHubClient.fromConnectionString(
    'Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key', 
    'myeventhub',
    { receiverLink: { attach: { maxMessageSize: 100000 }}});
client.createReceiver('$Default', '10', { startAfterTime: Date.now() })
    .then(function (rx) {
        rx.on('errorReceived', function (err) { console.log(err); }); 
        rx.on('message', function (message) {
            var body = message.body;
            var enqueuedTime = Date.parse(message.enqueuedTimeUtc);
        });
    });

Example 6 - Create a throttled receiver

Creates a receiver for a given partition ID, with a custom policy that throttles messages. Initially the client can deliver 10 messages, and after the "credit" goes below 5, the receiver will start allowing more messages only for those that have been "settled". Settling can be done via the settlement methods on EventData, such as accept or reject.

var EventHubClient = require('azure-event-hubs').Client;

var client = EventHubClient.fromConnectionString(
    'Endpoint=sb://my-servicebus-namespace.servicebus.windows.net/;SharedAccessKeyName=my-SA-name;SharedAccessKey=my-SA-key', 
    'myeventhub',
    EventHubClient.RenewOnSettle(10,5));
client.createReceiver('$Default', '10', { startAfterTime: Date.now() })
    .then(function (rx) {
        rx.on('errorReceived', function (err) { console.log(err); }); 
        rx.on('message', function (message) {
            var body = message.body;
            var enqueuedTime = Date.parse(message.enqueuedTimeUtc);
            // ... do some processing ...
            message.accept();
        });
    });