I create a realtime connection via SignalR From client(angular 9) and server(asp.net core 3.1) and Authorize hub by JWT Token such as below code :
private createConnection() {
this.hubConnection = new HubConnectionBuilder().withUrl(`${this.appConfig.hubEndpoint}/Hubs`,
{ accessTokenFactory: () => jwtToken })
.withAutomaticReconnect()
.build();
}
private startConnection(): void {
this.hubConnection
.start()
.then(() => {
this.connectionIsEstablished = true;
this.connectionEstablished.emit(true);
})
.catch(err => {
console.log('Error while establishing connection, retrying...');
});
}
this works fine until the token expired. According to my research, after receiving the new token with the refresh token, the previous connection should be stopped and a new connection should be created with the new token. Now I want to know how should I do this? Do I have to constantly check the token? Or should this be addressed by sending each request to the server?
unauthorized
and then you can restart the connection, didn't implement it yet but this is how I think it should be done – Kardon63undefined
value. It was also very hard for me to renew the connection because I am using an async call to get the token and I am wrapping the whole system with rxJS, so the quick and dirty fix was to reload the page when a disconnected event is received, while also setting aggressive delays for automatic reconnects. – Mehdi Benmoha