Created
November 20, 2017 16:43
-
-
Save XavierGeerinck/2db945fa32a42797d3d4fecedc882ade to your computer and use it in GitHub Desktop.
[Azure][Nodejs] Event Hub Send/Receive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EventHubClient = require("azure-event-hubs").Client; | |
const connectionString = '<ConnectionString>'; | |
const eventHubPath = '<EventHubName>'; | |
const client = EventHubClient.fromConnectionString(connectionString, eventHubPath); | |
// =================================== | |
// Open connection to the client | |
// =================================== | |
client | |
.open() | |
// =================================== | |
// Create Receivers | |
// =================================== | |
.then(() => client.getPartitionIds()) | |
.then((partitions) => { | |
partitions.forEach((partitionId) => { | |
console.log(`[Receiver - ${partitionId}] Created`); | |
client.createReceiver('$Default', partitionId, { startAfterTime: Date.now() }) | |
.then((receiver) => { | |
receiver.on('errorReceived', (err) => console.log(err)); | |
receiver.on('message', (message) => { | |
console.log(`[Receiver - ${partitionId}] ${JSON.stringify(message.body)}`); | |
// See http://azure.github.io/amqpnetlite/articles/azure_sb_eventhubs.html for details on message annotation properties from EH. | |
//var enqueuedTime = Date.parse(message.systemProperties['x-opt-enqueued-time']); | |
}); | |
}) | |
}); | |
return Promise.resolve(); | |
}) | |
// =================================== | |
// Create Sender | |
// =================================== | |
.then(() => client.createSender()) | |
.then((sender) => { | |
console.log('[Sender] Created'); | |
sender.on('errorSender', (err) => console.log(err)); | |
setInterval(() => { | |
let clientNumber = parseInt(Math.random() * 20 + 1); | |
sender.send({ | |
name: `client-${clientNumber}`, | |
isPremium: [1, 5, 7, 13, 20].indexOf(clientNumber) > -1 | |
}); | |
}, 1000); | |
//console.log(`[Sender] Sending client-${clientNumber} with isPremium: ${[1, 5, 7, 13, 20].indexOf(clientNumber) > -1}`); | |
}) | |
.catch((e) => console.log(e)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment