How to fix WebSocket connection error in Static web app

Chengxuan He 5 Reputation points
2024-06-26T13:29:05.42+00:00

I'm trying to connect websocket api.

The frontend is Next.js and it's on Azure static web app

And the backend is Django, it's on Azure app service.

I enabled CORS in the backend, so frontend in localhost has no problem when I connect websocket api of backend (it's on Azure App Service.).

I can't find any reason or message on the browser console why connection is failed.

WebSocket connection to 'wss://assessmate.azurewebsites.net/ws/notify/' failed: 
_app-456513e57728c64c.js:149 WebSocket error: 
Event {isTrusted: true, type: 'error', target: WebSocket, currentTarget: WebSocket, eventPhase: 2, …}
  isTrusted: true
  bubbles: false
  cancelBubble: false
  cancelable: false
  composed: false
  currentTarget: WebSocket {url: 'wss://assessmate.azurewebsites.net/ws/notify/?toke… 
  I6MX0.zgUVAmgIStnz7iIj8OXyIhPIK4QqvFbyvPNsjhal86A', readyState: 3, bufferedAmount: 0, onopen: ƒ, onerror: ƒ, …}
  defaultPrevented: false
  eventPhase: 0
  returnValue: true
  srcElement: WebSocket {url: 'wss://assessmate.azurewebsites.net/ws/notify/?toke… 
  I6MX0.zgUVAmgIStnz7iIj8OXyIhPIK4QqvFbyvPNsjhal86A', readyState: 3, bufferedAmount: 0, onopen: ƒ, onerror: ƒ, …}
  target: WebSocket {url: 'wss://assessmate.azurewebsites.net/ws/notify/?toke…I6MX0.zgUVAmgIStnz7iIj8OXyIhPIK4QqvFbyvPNsjhal86A', 
  readyState: 3, bufferedAmount: 0, onopen: ƒ, onerror: ƒ, …}
  timeStamp: 11759.199999999255
  type: "error"
  [[Prototype]]: Event

Please help us!

Azure Static Web Apps
Azure Static Web Apps
An Azure service that provides streamlined full-stack web app development.
966 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Grmacjon-MSFT 18,451 Reputation points
    2024-07-30T00:13:38.5933333+00:00

    Hi @Chengxuan He we are sorry to hear you're facing this issue. Is this a windows or Linux web app?

    Here are 3 things you can do to troubleshoot this issue:

    • Ensure that WebSockets are enabled in your Azure App Service. If your app is hosted on Linux, WebSockets are always enabled, and thus, you won’t find an option to turn them on or off. However, if its Windows based, you'll find the option present in the App's settings, Configuration tab.
    • Look into your CORS settings to ensure they are correctly configured to allow connections from your Azure Static Web App domain. This includes ensuring that the Access-Control-Allow-Origin header is set to allow your frontend domain.
    • Lastly,if your application relies on stateful connections, you might need to disable ARR Affinity in the General settings tab of your App Service. ARR Affinity can sometimes interfere with WebSocket connections

    Hope that helps. Please let us know if you have further questions.

    -Grace


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

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.