Azure IoT Hub : retrieving messages via browser-si

2019-05-14 16:43发布

问题:

Do you know if it is possible to retrieve device-to-cloud messages, with Microsoft Azure and IoT Hub, with a javascript client running in the browser (i.e. no node js) ?

It is my understanding that this is not currently possible, but I'd like to make sure (i.e. all current APIs can only work server side).

Edit1: REST APIs might be the obvious choice to attempt to do this; However, having tried these APIs via manual request, it seems the response does not provide the allow-access-control-origin header, so I can't call them from ajax due to CORS.

回答1:

UPDATE: The example code was removed by requirement from Azure IoT Hub team, and they do not like users to connect to IoT Hub from browsers (https://github.com/Azure/azure-iot-sdk-node/issues/112#issuecomment-338073636).

Sorry for the broken links in this answer.


It seems this is an old question, however, I find there is still nobody can give the right answer.

In short, is there a way to read IoT Hub D2C messages in browser? The answer is absolute YES!

You shouldn't use RESTful API to loop checking messages, but use AMQP over WebSockets.

Before you start, you must know that IoT Hub has two sides, Device side and Service side. For Device side, you connect to IoT Hub using HTTP, AMQP and MQTT, however, for Service side you can ONLY connect to IoT Hub using AMQP.

Fortunately, Red Hat had already written a AMQP over WebSockets Node module, and you can do some simple work to make it working in browser.

You can learn more about the AMQP Node module from https://github.com/grs/rhea.

Also, we've made a web based tool for receiving D2C messages https://iotdevtool.com/service/, you can learn more about it from https://blogs.msdn.microsoft.com/zhiqing/2017/03/16/connect-to-azure-event-hub-in-browser-using-amqp-over-websockets/.



回答2:

I haven't tried it myself but my understanding is that it's possible to retrieve cloud-to-device messages in JavaScript using the IoT Hub REST API.