Reputation: 26314
I'm trying to use Azure IoT Hub Device SDK for Node to listen to new blob in storage account events (or whatever that's called officialy). I'm following this sample.
I have receive notifications turned on:
However, the client SDK gets nothing when i add a file to that container.
client.on('message', function (msg) {
// message event does not fire
console.log('Id: ' + msg.messageId + ' Body: ' + msg.data);
}
If i send a cloud-to-device message (also from the Portal blade) it goes through just fine and i can read it in console.log()
.
Am i listening for the completely wrong event — message
?
Upvotes: 1
Views: 1364
Reputation: 8235
In addition to @pierreca-MSFT, there is also another option for upload file notifications such as using an Azure Event Grid (today in the preview). In this case, your files are uploaded into the event-driven blob storage, which automatically it will publish an event message to the Event Grid. These event messages can be distributed (pushed) to the subscribers in the reliable manner based on the Event Grid Subscriptions. This is an event-driven push model.
More details about the Azure Event Grid is here and for event-driven blob storage here.
The following screen snippet shows an example of the event message in my subscriber (Azure Function) after when the file has been uploaded from the device. Note, that for this example (for demonstration purpose), the device used a REST Api calls for handling an upload file, see more details here.
Upvotes: 0
Reputation: 183
It looks like you're expecting to receive file upload notifications on the cloud-to-device message channel. that's not where these notifications are surfaced.
There are 2 types of SDKs for Azure IoT Hub: Device SDKs, and Service SDKs.
In the Device SDK When the upload is complete, the callback to the uploadToBlob
method is called.
var Protocol = require('azure-iot-device-mqtt').Mqtt;
var Client = require('azure-iot-device').Client;
var fs = require('fs');
var filePath = '[path/to/file]';
var client = Client.fromConnectionString(deviceConnectionString, Protocol);
fs.stat(filePath, function (err, fileStats) {
var fileStream = fs.createReadStream(filePath);
client.uploadToBlob('testblob.txt', fileStream, fileStats.size, function (err, result) {
if (err) {
console.error('error uploading file: ' + err.constructor.name + ': ' + err.message);
} else {
console.log('Upload successful - ' + result);
}
fileStream.destroy();
});
});
If you want to listen for notification when a file is uploaded to a blob by a device, you need to use the Service SDK . Use the client to get a "file notification receiver" and then listen on this object's "message" event.
var Client = require('azure-iothub').Client;
var client = Client.fromConnectionString(connectionString);
client.open(function (err) {
if (err) {
console.error('Could not connect: ' + err.message);
} else {
console.log('Client connected');
client.getFileNotificationReceiver(function(err, receiver) {
if(err) {
console.error('Could not get file notification receiver: ' + err.message);
} else {
receiver.on('message', function(msg) {
console.log('File uploaded: ');
console.log(msg.data.toString());
receiver.complete(msg, function(err) {
if (err) {
console.error('Could not complete the message: ' + err.message);
} else {
console.log('Message completed');
}
});
});
}
});
}
});
Upvotes: 1