Azure / azure-sdk-for-c

This repository is for active development of the Azure SDK for Embedded C. For consumers of the SDK we recommend visiting our versioned developer docs at https://azure.github.io/azure-sdk-for-c.
MIT License
226 stars 120 forks source link

Inquiry: using MQTT-WS transport in azure-sdk-for-c-arduino #2593

Closed meta-space closed 1 year ago

meta-space commented 1 year ago

Query/Question I was wondering whether the MQTT-WS(S) transport is available in the azure-sdk-for-c-arduino library

Disclaimer: I'm asking here because the https://github.com/Azure/azure-sdk-for-c-arduino repo doesn't have an Issues section.

A tangential question was raised before: https://github.com/Azure/azure-sdk-for-c/issues/2313 There @danewalton stated that "[...] the transport is up to the user with this library, so you may swap in any MQTT or MQTT/WS library that you'd like."

I'm using azure-sdk-for-c-arduino lib on the ESP32 based on the Azure_IoT_Central_ESP32 example. The sample uses the esp_mqtt client and mqtts transport. But esp_mqtt also supports mqtt-ws (based on this esp_mqtt WSS sample).

If azure-sdk-for-c-arduino is fully transport-agnostic, it should be straightforward to switch to websockets (mqtt-ws or mqtt-wss). All that's required is a modified mqtt_client_config. I haven't had the time to try it out yet. Can anybody confirm my assumptions?

RLeclair commented 1 year ago

Hi @meta-space, you are correct. The transport layer is up to the user, you can refer to the esp_mqtt documentation to learn more about how to enable websockets.

By modifying the uri in mqtt_client_config you can connect via websockets. You need to change mqtt:// to wss:// as a prefix and add :443/$iothub/websocket as a suffix.

meta-space commented 1 year ago

@RLeclair Thank you for the confirmation. I'll try it out.

Won't I have to configure TLS Certificates in the esp_mqtt client when I switch to wss ?

RLeclair commented 1 year ago

No need, you should still be able to use the SAS tokens in the sample (I assume that is what you were using)

meta-space commented 1 year ago

@RLeclair Thank you! I've switched the protocol scheme from "mqtts://" to "wss://" and port number from 8883 to 443. But I haven't found a parameter or hook to easily change the suffix. Nonetheless, the device connects to the IoT Hub via DPS and is responsive via Azure IoT Explorer.

Is it perhaps possible that DPS will automatically redirect the device to "$iothub/websocket"?

Or asked differently: I've discovered the

define AZ_IOT_HUB_CLIENT_WEB_SOCKET_PATH "/$iothub/websocket"

in the az_iot_hub_client.h header, but I don't understand how to use it. I couldn't find any documentation or examples on the topic

RLeclair commented 1 year ago

@meta-space, great to hear! For your first question, DPS will provide the domain name for the IoT Hub, from there you can add the suffix and prefix to it.

With regard to the AZ_IOT_HUB_CLIENT_WEB_SOCKET_PATH, we don't have a sample on how to use it, but for our arduino samples you can edit our central sample (inside mqtt_client_init_function) and add something like (look at last line below):

az_span mqtt_broker_uri_span = AZ_SPAN_FROM_BUFFER(mqtt_broker_uri);
mqtt_broker_uri_span = az_span_copy(mqtt_broker_uri_span, AZ_SPAN_FROM_STR(MQTT_PROTOCOL_PREFIX));
mqtt_broker_uri_span = az_span_copy(mqtt_broker_uri_span, mqtt_client_config->address);
mqtt_broker_uri_span = az_span_copy(mqtt_broker_uri_span, AZ_SPAN_FROM_STR(":443/$iothub/websocket"));
meta-space commented 1 year ago

@RLeclair Thank you again. I used your snippet in the mqtt_client_init_function function and it appears to be working.