Created
September 22, 2017 21:01
-
-
Save vspedr/0fde41fc0103f4e18a1557fbb2f6e9c1 to your computer and use it in GitHub Desktop.
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
// based on: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-node-node-getstarted#receive-device-to-cloud-messages | |
const EventHubClient = require('azure-event-hubs').Client; | |
const connectionString = '{iothub connection string}'; | |
const printError = (err) => { | |
console.err(err.message); | |
} | |
const printMessage = (message) => { | |
console.log('Message received: '); | |
console.log(JSON.stringify(message.body)); | |
console.log(''); | |
} | |
const client = EventHubClient.fromConnectionString(connectionString); | |
client.open() | |
.then(client.getPartitionIds.bind(client)) | |
.then((partitionIds) => { | |
return partitionIds.map((partitionId) => { | |
return client.createReceiver('$Default', partitionId, {'startAfterTime': Date.now()}) | |
.then((receiver) => { | |
console.log('Created partition receiver: ' + partitionId); | |
receiver.on('errorReceived', printError); | |
receiver.on('message', printMessage) | |
}) | |
}) | |
}) | |
.catch(printError); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment