API Gateway WebSocket Connection Issue: Client's OnClosed Event Not Triggered Upon Disconnection/Timeout

0

Hello everyone,

I'm currently experiencing a perplexing issue with AWS API Gateway's WebSocket service where the WebSocket connection gets disconnected by the API Gateway, but the expected OnClosed event does not trigger on the client-side, with the same logic on the client but connecting to my node.js WebSocket server on my local machine the OnClosed event does trigger, so why isn't it triggering when disconnected from the API Gateway? I'm hoping to find some insights or solutions from anyone who might have faced a similar issue or has expertise with WebSockets and AWS API Gateway.

--Issue Description-- I've set up a WebSocket API using AWS API Gateway to facilitate real-time communication between my client application and server. The WebSocket connection is established without any issues, and both message sending and receiving work as expected with the appropriate client events being triggered for OnConnected, OnMessageReceived etc. However, when the API Gateway closes the WebSocket connection (e.g., due to the 2-hour connection limit, 10 minute timeout or manual disconnection from the server side), my client application does not seem to detect this disconnection through the standard OnClosed WebSocket event.

--Expected Behavior-- Upon disconnection initiated by AWS API Gateway (either due to timeout or server-side action), I expect the client's WebSocket OnClosed event to be executed, allowing my client application to handle the disconnection gracefully (e.g., by pooling messages that need to be sent and attempting reconnection or alerting my management service).

--Actual Behavior-- The WebSocket connection is terminated by the API Gateway, but the client-side OnClosed event handler does not get executed. There are no errors or exceptions thrown on the client side; it's as if the disconnection goes unnoticed by the client WebSocket implementation.

--Notes-- I have also tried connecting to the API Gateway via a client WebSocket node.js app and that application has the same results by also not executing the OnClosed event. I also read that someone else using API Gateway WebSocket but with a different issue API Gateway sends a status code "1001 - Going away" when they timeout or disconnect.

I'm at a loss for why the OnClosed event isn't being triggered upon disconnection by the API Gateway. If anyone has insights, suggestions, or solutions based on your experiences, I would greatly appreciate your input.

Thank you in advance for your help!

1 Answer
0

Upon disconnection initiated by API Gateway, the client's WebSocket OnClosed event should be executed to handle the disconnection gracefully.

  • Check API Gateway configuration for correct settings, including timeouts and connection limits.
  • Verify WebSocket client implementation to ensure proper handling of WebSocket events.
  • Test with different WebSocket clients to identify if the issue is specific to the client implementation.
  • Inspect WebSocket close codes to determine the reason for closure and ensure proper handling.
  • Review AWS documentation, forums, and support channels for relevant information and solutions.
  • Contact AWS Support for personalized assistance if unable to resolve the issue independently.
  • Consider alternative solutions for real-time communication if the issue persists.
profile picture
EXPERT
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions