This repository has been archived by the owner on Apr 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
azure-event-hub-receive.js
65 lines (55 loc) · 2.78 KB
/
azure-event-hub-receive.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
var EventHubClient = require('azure-event-hubs').Client;
module.exports = function (RED) {
function AzureEventHubReceiveNode(config) {
RED.nodes.createNode(this, config);
var node = this;
node.connectionstring = config.connectionstring;
node.consumergroup = config.consumergroup;
// clear status, might be left over after updating settings
node.status({});
try {
var client = EventHubClient.fromConnectionString(node.connectionstring);
client.open()
.then(client.getPartitionIds.bind(client))
.then(function (partitionIds) {
var errorReceived = false;
return partitionIds.map(function (partitionId) {
// loop over all partitions of the Event Hub
return client.createReceiver(config.consumergroup, partitionId, { 'startAfterTime': Date.now() }).then(function (receiver) {
// succesfully connected to a partition
if (!errorReceived) { // it could be only one partition receiver gives an error, hence the check
node.log('Created partition receiver: ' + partitionId);
node.status({ fill: "green", shape: "ring", text: "connected" });
}
receiver.on('errorReceived', function (err) {
errorReceived = true;
node.status({ fill: "yellow", shape: "ring", text: "error received, see debug or output" });
node.error(err.message);
});
receiver.on('message', function (receivedMessage) {
// message received from Event Hub partition
var msg = { payload: receivedMessage.body }
node.send(msg);
});
});
});
})
.catch(function (err) {
node.status({ fill: "red", shape: "ring", text: "unexpected error: " + err.message });
node.error(err.message);
});
this.on('close', function (done) {
node.log('closing ...');
client.close().done(function () {
node.log('closing done.');
done();
});
});
}
catch (err) {
this.error(err.message);
node.status({ fill: "red", shape: "ring", text: "can't connect, " + err.message });
}
}
RED.nodes.registerType("azure-event-hub-receive", AzureEventHubReceiveNode);
}