Why do I get this error: .js:109 WebSocket connection to 'wss://heat-sensor-system.azure-devices.net/' failed?

Denitsa Radeva 0 Reputation points
2023-05-08T22:07:44.04+00:00

In my react app I put the exact connection string for the device I want to fetch data from. However, I get this error: .js:109 WebSocket connection to 'wss://heat-sensor-system.azure-devices.net/' failed? when I try to connect with:

azure-iot-device.Client.open()
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,272 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AshokPeddakotla-MSFT 35,971 Reputation points Moderator
    2023-05-09T03:17:06.52+00:00

    @Denitsa Radeva Welcome to Microsoft Q&A forum!

    Coul you please confirm which documentation are you following?

    In my react app I put the exact connection string for the device I want to fetch data from. However, I get this error: .js:109 WebSocket connection to 'wss://heat-sensor-system.azure-devices.net/' failed?

    WebSocket connection failed error can occur due to multiple reasons.

    As you have already mentioned, you provided an exact connection string. Please double check the below details.

    • Make sure that you have provided the correct connection string for the device you want to fetch data from. You can check the connection string in the Azure portal by going to the IoT Hub, selecting the device as shown below. User's image
    • Check if the device is registered in the IoT Hub. You can check this by going to the IoT Hub, selecting the "IoT devices" tab, and then look for the device you are trying to connect to.
    • Check if the device is online and sending data to the IoT Hub. You can check this by going to the IoT Hub, If the device is online and sending data, you should see some telemetry data in the "Messages" tab.
    • You can use a tool like Fiddler or Wireshark to capture network traffic: If the issue is related to network connectivity or communication between your IoT device and the web app, capturing network traffic can help you see what's going on and identify any issues.
    • If you have access to another IoT device, you can try using it to send data to the IoT Hub and see if the issue persists.

    Azure IoT device SDKs disconnect from IoT Hub and then reconnect when they renew SAS tokens over the MQTT (and MQTT over WebSockets) protocol. In logs, this shows up as informational device disconnect and connect events sometimes accompanied by error events.

    By default, the token lifespan is 60 minutes for all SDKs; however, it can be changed by developers in some of the SDKs. The following table summarizes the token lifespan, token renewal, and token renewal behavior for each of the SDKs:

    User's image

    For more details, please refer to MQTT device disconnect behavior with Azure IoT SDKs

    If you are running the React app on a local machine or behind a firewall, check the firewall settings to make sure that the necessary ports are open. You may need to open port 443 (for HTTPS traffic) or port 8883 (for MQTT traffic) to allow the WebSocket connection to Azure IoT Hub.

    If you have checked all of these and still having trouble connecting to the IoT Hub, you may want to try using a different protocol, such as HTTP or AMQP, to see if that resolves the issue. See Choose a device communication protocol for more details.

    Also, please enable logging and check this troubleshooting guide.

    Do let us know if that helps or have any further queries. Happy to assist you.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.