Azure / azure-iot-sdk-node

A Node.js SDK for connecting devices to Microsoft Azure IoT services
https://docs.microsoft.com/en-us/azure/iot-hub/
Other
261 stars 227 forks source link

[Technical Question] File upload notification receive on multiple listeners #918

Closed amit12cool closed 3 years ago

amit12cool commented 3 years ago

I'm trying to subscribe to n number of IoT hub file notification handlers using Azure IoT hub sdk for nodejs. And there are 2 consumers who are subscribing to same IoT hub file notification handlers. So each each IoT hub has 2 consumers subscribed to file upload notifications. Below is my code running on 2 consumers i.e. nodejs apps.

    public async registerFileUploadNotifiers(): Promise<void> {
        // get all iotHubs configured
        const iotHubs = this.azureIotHubHelperService.getAllIoThubs();
        await Promise.all(iotHubs.map(async (iotHub) => {
            await this.listenToFileUploadEvent(iotHub.connectionString).catch((error) => {
                this.appLoggerService.error(
                    `Could not register file upload listener for iotHub: ${JSON.stringify(iotHub)}`,
                    IoTHubFileUploadEventService.name
                );
                this.appLoggerService.error(
                    error,
                    IoTHubFileUploadEventService.name
                );
            });

        }));
    }

    private async listenToFileUploadEvent(connectionString: string): Promise<void> {
        const serviceClient = Client.fromConnectionString(connectionString);
        await serviceClient.open();
        const notificationReceiver = await serviceClient.getFileNotificationReceiver();
        notificationReceiver.result.on('message', async (event) => {
            // my business logic
        });
    }
}

The issue I face is notificationReceiver.result.on event is never called even if the file was uploaded correctly to the IoT hub. Only once in a blue moon I get a notification. Am I missing something?

anthonyvercolano commented 3 years ago

Hello Amit,

The file notification queue for an IoT Hub is shared resource over the whole hub. It will be shared by all clients. What happens if you have a single consumer for each hub. What happens with your notification then?

amit12cool commented 3 years ago

Thanks Anthony. I ran the sample code given in Azure docs with three consumers and one file uploader.The file upload notification arrived at one of the consumer one by one. So for 3 consumers the notification came consecutively and then on the fourth time it didn't came.The fifth time it came on the 1st consumer. So my 2 observations were below:-

1) File upload notification came on one of the consumer out of 3 consumers registered. Would you like to give any comments on this? 2) Every 4th notification was not received by any of the consumer.Can you run this use case?

Get Outlook for Androidhttps://aka.ms/ghei36


From: Anthony V. Ercolano notifications@github.com Sent: Friday, January 15, 2021 4:06:27 AM To: Azure/azure-iot-sdk-node azure-iot-sdk-node@noreply.github.com Cc: Amit Dhawan amit.dhawan@outlook.com; Author author@noreply.github.com Subject: Re: [Azure/azure-iot-sdk-node] [Technical Question] File upload notification receive on multiple listeners (#918)

Hello Amit,

The file notification queue for an IoT Hub is shared resource over the whole hub. It will be shared by all clients. What happens if you have a single consumer for each hub. What happens with your notification then?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/Azure/azure-iot-sdk-node/issues/918#issuecomment-760520614, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABTB2JCJZL5PCEQF7Z22V43SZ5WWXANCNFSM4WA3WJSA.

anthonyvercolano commented 3 years ago

Which version of the service client package (azure-iothub) are you using? Version 1.13.1 is the most current.

vishnureddy17 commented 3 years ago

Also, what version of Node are you running? We are trying to reproduce the issue.

amit12cool commented 3 years ago

I'm using 1.13.1

Get Outlook for Androidhttps://aka.ms/ghei36


From: Anthony V. Ercolano notifications@github.com Sent: Saturday, January 16, 2021 3:46:56 AM To: Azure/azure-iot-sdk-node azure-iot-sdk-node@noreply.github.com Cc: Amit Dhawan amit.dhawan@outlook.com; Author author@noreply.github.com Subject: Re: [Azure/azure-iot-sdk-node] [Technical Question] File upload notification receive on multiple listeners (#918)

Which version of the service client package (azure-iothub) are you using? Version 1.13.1 is the most current.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/Azure/azure-iot-sdk-node/issues/918#issuecomment-761226844, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ABTB2JGVQTOIY7ENLYH4NPDS2C5FRANCNFSM4WA3WJSA.

anthonyvercolano commented 3 years ago

The node version would be something like 10.12.1 or 12.16.0 or 14.5.0

Which sample/doc are you using?

Does your code use the complete on the message?

anthonyvercolano commented 3 years ago

Are you just doing file uploads to a single hub and experiencing this problem?

vishnureddy17 commented 3 years ago

1) File upload notification came on one of the consumer out of 3 consumers registered. Would you like to give any comments on this?

As @anthonyvercolano mentioned, the file notification queue for an IoT Hub is shared resource over the whole hub. This means that only one listener will receive the notification for a particular file upload.

2) Every 4th notification was not received by any of the consumer.Can you run this use case?

We tried to recreate your example code as best we can to reproduce the issue:

'use strict';

const Client = require('azure-iothub').Client;
const uuid = require('uuid');

async function registerFileUploadNotifiers() {
    // get all iotHubs configured
    const iotHubs = [
        //put iothub connection strings here
    ];
    await Promise.all(iotHubs.map(async (connectionString) => {
        await listenToFileUploadEvent(connectionString).catch((error) => {
            console.log(
                `${error}: ${JSON.stringify(connectionString)}`,
            );
        });
    }));
};

async function listenToFileUploadEvent(connectionString) {
    const serviceClient = Client.fromConnectionString(connectionString);
    await serviceClient.open();
    const listenerId = uuid.v4();
    const notificationReceiver = await serviceClient.getFileNotificationReceiver();
    notificationReceiver.result.on('message', async (event) => {
        console.log(`id: ${listenerId} file was uploaded: ${event.data.toString()}`);
    });
}

async function main() {
    await registerFileUploadNotifiers()
}

main().then(() => console.log("all good")).catch(() => console.log("something wrong"));

When running three instances of this program, we are seeing file upload notifications being received as expected with no missing notifications. Do you see anything different with our approach that could be preventing us from reproducing the issue? It is also possible that there is an extraneous 4th listener receiving notifications which can make it seem like every 4th notification is not being received.

vishnureddy17 commented 3 years ago

Hi, @amit12cool. We will assume that your problem has been mitigated on Wednesday, February 3rd.

vishnureddy17 commented 3 years ago

Closing this issue. @amit12cool, feel free to reopen if this is still a problem.