Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

941
Vistas
How to handle 401 errors in@microsoft/signalr JavaScript client

We are using the @microsoft/signalr JavaScript client in our Vue frontend to establish a websocket connection with our backend (.net core). Also, we use a Bearer token for authentication.

This is the connection builder in the frontend:

this.connection = new HubConnectionBuilder()
.withUrl('/chat', { accessTokenFactory: () =>  IdService.getToken() })
.configureLogging(LogLevel.Information)
.withAutomaticReconnect()
.build()

Then I start the connection in this code:

HubService.connection.start().then(() => {
      console.log('Connection started')
    }).catch(err => {
      console.error(err)
    })

My problem is, when the Bearer token expires in the backend, on a reconnect I get a 401 error in the frontend, which is correct and I want to respond correctly to this error. I can catch the error in the catch block of the start function but I don't know how to handle the error, because I can't read a status code from the request like in a normal HTTP request. Its just a error message from the signalr client. Of course I could search in the string for '401' but that seems wrong.

Error: Failed to start the connection: Error: Failed to complete negotiation with the server: Error: Unauthorized: Status code '401'

I would like to know where in my code and how to properly handle this type of error and other errors to. Any help or ideas are appreciated.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The correct way to to this is your IdService.getToken() get you a refreshed token when it is almost expires because it's said in the Microsoft documentation that this function is called every time that there is an communication from client to hub.

The access token function you provide is called before every HTTP request made by SignalR. If you need to renew the token in order to keep the connection active (because it may expire during the connection), do so from within this function and return the updated token.

There are many examples for the token providers to refresh the token when it is near expiration.

But if that fix/implementation is out of your reach/control, the only thing you can do is catch the exception and just initialize again the connection. Something like this:

  1. Catch the exception;
  2. Emit an event with 401 unauthorized exception.
  3. Catch the event and just call the method that initializes your connection.

But this is a workaround to the original problem, that is the proper token refresh function.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda