react-use-websocket icon indicating copy to clipboard operation
react-use-websocket copied to clipboard

Handle exceeding AWS max connection duration

Open kaybee99 opened this issue 3 years ago • 6 comments

Hi,

API gateway has a max connection duration of 2 hours on all websocket endpoints (https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html#apigateway-execution-service-websocket-limits-table). This limit can't be increased.

The auto reconnect option handles the 10 minute Idle Connection Timeout perfectly, but seemingly not the 2 hour limit Is there something that can be done to mitigate this?

Thanks

kaybee99 avatar Jul 07 '21 08:07 kaybee99

Hi @kaybee99,

Could you post an example of how you are using useWebSocket? I can't think of a reason why it would not reconnect for the 2 hour limit (and how difficult that would be to test!), but before I start guessing, I would like to see if I can infer anything from your setup.

robtaussig avatar Jul 08 '21 19:07 robtaussig

Using it like this

const { sendJsonMessage } = useWebSocket(socketUrl, {
    onMessage: (message) => handleWebsocketEvent(message),
    queryParams: {
      Authorizer: token,
      locationId: selectedLocationId
    },
    shouldReconnect: (_closeEvent) => true,
    onClose: () => console.log("closing"),
    onError: (err) => console.log("error", err),
  })

Yes, you're right. Testing is a pain!

When the two hour mark is reached the websocket connection starts to fail (Websocket connection to wss://... failed), then onError() is called followed by onClose(). This happens repeatedly until the reconnection attempt limit is reached

kaybee99 avatar Jul 09 '21 07:07 kaybee99

Is it possible that AWS requires a new token after the 2 hour limit?

Also, a few things that I would try:

const { sendJsonMessage } = useWebSocket(socketUrl, {
    onMessage: (message) => handleWebsocketEvent(message),
    queryParams: {
      Authorizer: token,
      locationId: selectedLocationId
    },
    shouldReconnect: (_closeEvent) => true, //what does the close event look like when caused by the 2 hour limit? Is there a different error code that you could use? I would look into returning false in this case and adding the following option:
    retryOnError: true, //With this true, and returning false in the 'shouldReconnect' will avoid trying to reconnect twice for the same event
    onClose: () => console.log("closing"),
    onError: (err) => console.log("error", err),
  })

robtaussig avatar Jul 09 '21 14:07 robtaussig

The token is still valid. I can use it to authenticate with other rest endpoints without a problem.

The close event in this instance has status 1006 with empty reason. It feels like API gateway kills the connection to the browser when the 2 hour limit is reached. Refreshing the page fixes it probably because as far as APIg is concerned it's a new connection altogether (ie another client ID).

I'll try and force the component to rerender on close when the status is 1006. See if that has any effect

kaybee99 avatar Jul 10 '21 22:07 kaybee99

Just wanted to add that react-use-websocket is handling the API gateway max connection duration for me. We're keeping our token fresh after every reconnect but that's all.

Anntoin avatar Nov 05 '21 13:11 Anntoin

@kaybee99 Have you found the solution for this? I'm having the exact same problem

hoandang avatar Sep 02 '23 13:09 hoandang