locked
EventHubClient open function call gives Bad Request Error on cloud nodejs web app RRS feed

  • Question

  • I have tried ReadDeviceToCloudMessages.js example with a node js web app on cloud. When I started it on console it gave bad request error. When I tried exactly same example on my pc and started it, it worked completely fine. Shouldn't EventHubClient run on cloud at app Back end? What am I doing wrong?

    Here is the ReadDeviceToCloudMessages.js:

    'use strict';var EventHubClient = require('azure-event-hubs').Client;var connectionString = '{connectionstring}';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);

     
    Wednesday, April 19, 2017 2:42 PM

All replies

  • Hi, NilKaradag

    Please tell us about the following.

    * Detailed error message / state
    * Web App Environment on the cloud
    * Firewall settings on the cloud (Did you enable the port for connecting IoT Hub?)

    Y.Nakata

    Sunday, April 30, 2017 2:56 PM
  • Hi Yoshitaka,

    I am using a web app on the cloud to run a nodejs server. And I didn't enable firewall settings on the cloud. So it might be because of that.

    This is the error message:

    server responded with a status of 500 (Internal Server Error)

    Could you tell me how I can enable the port for connecting IoT Hub? 

    Many thanks,

    Nil


    • Edited by NilKaradag Friday, May 5, 2017 1:40 PM
    Friday, May 5, 2017 7:06 AM
  • Hi, NilKaradag

    Maybe the port setting is not a problem.

    I created the Web app on the Azure in the following steps.

    The app is able to communicate with IoT hub.

    Please refer to that.

    1. Set up IoT Hub (https://github.com/Azure/azure-iot-sdk-csharp/blob/master/doc/setup_iothub.md)

    2. Create device setting to IoT Hub using the Device Manager (https://github.com/Azure/azure-iot-sdk-csharp/tree/master/tools/DeviceExplorer#createdevice)

    3. Create Node.js App using VS2017 (https://koukia.ca/node-js-express-4-application-in-visual-studio-2017-and-azure-web-apps-c86b67582cf4)

    4. Add 'azure-event-hubs' package to project from npm

    5. Add following code for connecting to IoT Hub in App.js (ref. ReadDeviceToCloudMessages.js)

    var EventHubClient = require('azure-event-hubs').Client;
    var connectionString = '{connection string}';
    
    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);

    6. Create App Service (Web Apps) on Azure

    7. Publish Node.js App to App Service (Web Apps) on Azure (https://koukia.ca/node-js-express-4-application-in-visual-studio-2017-and-azure-web-apps-c86b67582cf4)

    Y.Nakata

    Sunday, May 14, 2017 1:36 PM