0

We have a React TypeScript front-end that is connecting to a WebSocket server behind an AWS API Gateway. The front-end passes a valid Cognito JWT access token as a query string into the Websocket connection url. When the token expires we were expecting the error response in the front-end/browser to have a status code of 401.

However, the error we receive is a generic Websocket error -

WebSocket connection to 'wss://...' failed:

The corresponding close event object has a a status code of 1006, with no reason given.

It appears this is because the reading of Websocket HTTP status codes is not supported by the Websocket protocol itself - How to read status code from rejected WebSocket opening handshake with JavaScript?

Do you know of good practice approaches we could use to handle this websocket auth error response in the front-end?

We are considering an approach where we verify the token (using an aws library) in the front-end BEFORE making the actual request to connect. We would then refresh it if it had already expired before making the original request with the refreshed token. If the request continued to fail then we would assume we had an error with the server that was not auth.

Anyhow, just wondering what others had done in this situation....

Thanks,

Sam

SamBrick
  • 611
  • 1
  • 6
  • 29
  • These posts actually cover similar issues and solutions and have been useful. Adding here for posterity: https://stackoverflow.com/questions/43788131/jwt-verify-client-side and https://stackoverflow.com/questions/41534287/whats-the-best-practice-to-renew-a-token-for-a-websocket-connection – SamBrick May 31 '22 at 07:35

0 Answers0