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

Unable to browserify a working version of azure-iot-device-* #112

Closed erikwilson closed 6 years ago

erikwilson commented 7 years ago

Context

Description of the issue:

When attempting to use a browserified version of an azure-iot-device the client is unable to establish a connection. This should be possible as repos like https://github.com/azure-iothub/v2 are capable of connecting MQTT over websockets.

When using a browserified version of azure-iot-device-http the connection is refused due to CORS issues (also see https://github.com/Azure/azure-event-hubs-node/issues/11).

Code sample exhibiting the issue:

const Device = require('azure-iot-device')
const Client = require("azure-iot-device-mqtt").MqttWs
const Message = Device.Message;

const clientFromConnectionString = function (connectionString) {
  return Device.Client.fromConnectionString(connectionString, Client);
}

const connectionString = '...'

const client = clientFromConnectionString(connectionString);

const handleMessage = function (msg) {
    client.complete(msg)
    var message = msg.data.toString()
    try {
      message = JSON.parse(msg.data)
    } catch(e) {}
    console.log(JSON.stringify(message,null,2))
}

client.on('message',handleMessage)

client.open(function(err) {
  if (err) {
    console.error('Could not connect: ' + err);
    return
  }
  client.sendEvent(new Message('hello from the browser'))
})

console.log('connectionString:', connectionString)

Console log of the issue:

For MqttWs and AmqpWs a websocket connection is attempted but fails with:

bundle.js:27 Could not connect: NotConnectedError: Unable to establish a connection

For Http:

VM503:1 OPTIONS https://ssfcpoc.azure-devices.net/devices/powermate-example/messages/events?api-version=2017-06-30 405 (Method Not Allowed)
(index):1 Failed to load https://ssfcpoc.azure-devices.net/devices/powermate-example/messages/events?api-version=2017-06-30: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:9966' is therefore not allowed access. The response had HTTP status code 405. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
pierreca commented 7 years ago

@erikwilson

your observations are correct.

As far as service is concerned, CORS is not supported and there's no short-term plan to make this available.

Regarding the SDK, compatibility with browserify never was a goal of the SDK: it is a Node.js SDK, not a generic Javascript SDK. This being said, we're always open to feedback and pull requests. Since it's a sizable chunk of work, if you would like to take it on, I would suggest we talk first about ways to achieve that especially if you want to contribute it back.

What's the scenario that you are trying to build, if I may ask?

erikwilson commented 7 years ago

The MQTT websocket issue seems to be something other than CORS since there are unofficial examples of MQTT websocket code that are able to connect as a device (https://github.com/azure-iothub/v2).

Right now I am just attempting to setup a virtual device in the browser capable of sending and receiving messages for a broader IOT scenario and would rather not create a service to relay information.

pierreca commented 7 years ago

OK, it's a simulator app. That is indeed the most common use case for this request.

Like I said we never structured the SDK to work in the browser. i'm not saying it's not possible, just that we have a lot more stuff on our list of problems to solve that come before that. I'm not against helping with experiments and even taking contributions that would add the feature back into the SDK, but I also need to clearly set expectations: it's unlikely that we'll add this feature ourselves in the short term.

erikwilson commented 7 years ago

I'm not creating a simulator app, I'm creating a dashboard and a control panel for a real IOT scenario. The person who created that repo also wrote a series of blog posts about using MQTT over websockets through the browser, and created that repo as an example. If you can't support virtual devices in the browser as a feature right now that's okay, we will come up with another solution to the issue. Thanks tho.

pierreca commented 6 years ago

Flagging this as wontfix since there's no chance we'll get to that anytime soon and it requires service changes anyway. we may revisit this later.

francois-nitenberg commented 6 years ago

Is there any alternative for ChromeOS? Would WebAssembly be fine with the C SDK?

timmyreilly commented 4 years ago

@pierreca curious if there's any update? With Blazor out, managing IoT Devices from the Web with the C# SDKs would be sweet.

anthonyvercolano commented 4 years ago

@timmyreilly The ability to run the sdk in a browser has moved considerably higher in the backlog, but I feel pretty confident that we wouldn't start work on it during the first half of calendar 2020.

timmyreilly commented 4 years ago

Is there anyway of extending HttpTransportSettings class to set the requests' mode? image

pelikhan commented 3 years ago

@anthonyvercolano Is there still a plan to support CORS for the Azure IoT Hub REST services?