WebSocket Connection Closed Prematurely on Azure App Service

Domenico Zurlo 1 Reputation point
2023-11-06T10:30:17.5033333+00:00

Hello,

I'm encountering an issue with my application hosted on Azure App Service. The application utilizes WebSockets, and I've been consistently receiving the following error: "WebSocket connection was closed prematurely: The remote party closed the WebSocket connection without completing the close handshake." This issue only arises when the app is deployed on Azure, not in the local environment.

Here are the details:

  • App Service Plan: [P2v3]
  • Runtime Stack: [ .NET]
  • Region: [West Europe]

The "Always On" setting is active.

Could you please assist me in diagnosing the potential cause of this issue? Are there any specific security settings within Azure that I should review or adjust to ensure stable WebSocket connections?

Thank you for your assistance.

Best regards,

Domenico.

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,212 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Grmacjon-MSFT 17,136 Reputation points
    2023-11-09T04:54:39.6333333+00:00

    Hi @Domenico Zurlo We are sorry to hear you're facing this issue. Based on the information you shared, it seems that your WebSocket connection is being terminated by Azure due to an idle timeout

    Here are some potential causes and solutions for this issue:

    Idle timeout: Azure App Service has an idle timeout setting that closes connections that have been idle for a certain amount of time. This can cause WebSocket connections to be closed prematurely. To resolve this issue, you can increase the idle timeout setting. You can do this by setting the WEBSITES_CONTAINER_IDLE_TIMEOUT app setting to a higher value. The default value is 20 minutes, but you can increase it up to 1800 seconds (30 minutes)

    WebSockets not enabled: By default, Azure App Service does not enable WebSockets. You need to explicitly enable WebSockets in your app. To do this, you can add the WEBSOCKETS_ENABLED app setting and set it to true. You can also enable WebSockets in the Azure portal by going to your app's Configuration > General settings > Web sockets and turning on the Web sockets switch

    Firewall or proxy blocking WebSocket traffic: If your app is behind a firewall or proxy, it may be blocking WebSocket traffic. You can try adding an exception to allow WebSocket traffic through the firewall or proxy. You can also try using a different port for WebSocket traffic, as some firewalls and proxies may block traffic on certain ports

    1 person found this answer helpful.
    0 comments No comments