Created
March 22, 2017 09:55
-
-
Save Sneezry/7f823cc167c37209c4e773419008347f 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
// Copyright (c) Microsoft. All rights reserved. | |
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | |
'use strict'; | |
var EventHubClient = require('azure-event-hubs').Client; | |
var Promise = require('bluebird'); | |
// The Event Hubs SDK can also be used with an Azure IoT Hub connection string. | |
// In that case, the eventHubPath variable is not used and can be left undefined. | |
var connectionString = 'HostName=zhe-iot-hub-temperature-demo.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=1vzP...vGoc='; | |
var eventHubPath = 'messages/events/'; | |
var printError = function (err) { | |
console.error(err.message); | |
}; | |
var printEvent = function (ehEvent) { | |
console.log('Event Received: '); | |
console.log(JSON.stringify(ehEvent.body)); | |
console.log(''); | |
}; | |
var client = EventHubClient.fromConnectionString(connectionString, eventHubPath); | |
var receiveAfterTime = Date.now() - 5000; | |
client.open() | |
.then(client.getPartitionIds.bind(client)) | |
.then(function (partitionIds) { | |
return Promise.map(partitionIds, function (partitionId) { | |
return client.createReceiver('$Default', partitionId, { 'startAfterTime' : receiveAfterTime}).then(function(receiver) { | |
receiver.on('errorReceived', printError); | |
receiver.on('message', printEvent); | |
}); | |
}); | |
}) | |
.then(function() { | |
return client.createSender(); | |
}) | |
.catch(printError); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment