forked from Azure/azure-iot-sdks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_batch_http.js
34 lines (26 loc) · 1.17 KB
/
send_batch_http.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
31
32
33
34
// 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 clientFromConnectionString = require('azure-iot-device-http').clientFromConnectionString;
var Message = require('azure-iot-device').Message;
// String containing Hostname, Device Id & Device Key in the following formats:
// "HostName=<iothub_host_name>;DeviceId=<device_id>;SharedAccessKey=<device_key>"
var connectionString = '[IoT Hub device connection string]';
var client = clientFromConnectionString(connectionString);
// Create two messages and send them to the IoT hub as a batch.
var data = [
{ id: 1, message: 'hello' },
{ id: 2, message: 'world' }
];
var messages = [];
data.forEach(function (value) {
messages.push(new Message(JSON.stringify(value)));
});
console.log('sending ' + messages.length + ' events in a batch');
client.sendEventBatch(messages, printResultFor('send'));
function printResultFor(op) {
return function printResult(err, res) {
if (err) console.log(op + ' error: ' + err.toString());
if (res) console.log(op + ' status: ' + res.statusCode + ' ' + res.statusMessage);
};
}