Skip to content

Instantly share code, notes, and snippets.

@vspedr
Created September 22, 2017 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vspedr/0fde41fc0103f4e18a1557fbb2f6e9c1 to your computer and use it in GitHub Desktop.
Save vspedr/0fde41fc0103f4e18a1557fbb2f6e9c1 to your computer and use it in GitHub Desktop.
// 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