-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadDeviceToCloudMessages.js
30 lines (26 loc) · 1.35 KB
/
ReadDeviceToCloudMessages.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
'use strict';
var EventHubClient = require('azure-event-hubs').Client;
//var connectionString = `HostName=bsp4iot.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=OqyO2vcRUYurLu/qBIMpP2gzuwn8RxZjtMogTlbYfbM=`;
//var connectionString =`HostName=BSWAzureIoTHub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=llBYLiEjOqKZrl16CMY5nSL+9MSVkKCZ9Iy0erLFalE=`
var connectionString = `HostName=XDKHub.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=L6SOK0MtahYHdnAIcKTlSWscFiF/vpoDx/IwgS1DLoI=`
var printError = function (err) {
console.log(err.message);
};
var printMessage = function (message) {
console.log('Message received: ');
console.log(JSON.stringify(message.body));
console.log('');
};
var client = EventHubClient.fromConnectionString(connectionString);
client.open()
.then(client.getPartitionIds.bind(client))
.then(function (partitionIds) {
return partitionIds.map(function (partitionId) {
return client.createReceiver('$Default', partitionId, { 'startAfterTime': Date.now() }).then(function (receiver) {
console.log('Created partition receiver: ' + partitionId)
receiver.on('errorReceived', printError);
receiver.on('message', printMessage);
});
});
})
.catch(printError);