Azure / azure-signalr

Azure SignalR Service SDK for .NET
https://aka.ms/signalr-service
MIT License
427 stars 101 forks source link

onClose method not get triggered when the access token is expired #1954

Closed Manojkumar226 closed 2 months ago

Manojkumar226 commented 5 months ago

Describe the bug

Context: We are using a serverless azure signalR service at the backend(python) and at client side we are using the @microsoft/signalR library for the react application. Below is the code included in the client side. Initally when the app gets loaded the signalR connection is established successfully. when a accesstoken is expired in background, the onClose call back is not triggered where we are trying to fetch the new access token and start the connection automatically.


interface HookReturnTypes {
  signalRHubConnection: HubConnection
}

const useSignalRInitialization = (): HookReturnTypes => {
  const getNewAccessTokenAPI = useGetSignalRAccessToken()
  const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null)

  const signalRConnection = new HubConnectionBuilder()
    .withUrl(getSignalRConnectionUrl() , {
      accessTokenFactory: () => getSignalRAccessToken() ,
      skipNegotiation: true,
      transport: HttpTransportType.WebSockets
    })
    .configureLogging(LogLevel.Information)
    .build()

  const startConnection = async (): Promise<void> => {
    try {
      await signalRConnection.start()
      console.log('Connected to SignalR')
    } catch (err) {
      console.error('Error connecting to SignalR:', err)
      await getNewAccessTokenAPI.triggerAPI({
        deviceId: getDeviceId()
      })

      timeoutRef.current = setTimeout(startConnection, 5000)
    }
  }

  useEffect(() => {
    startConnection()

    signalRConnection.onclose(async () => {
      await startConnection()
    })

    return () => {
      signalRConnection.stop()
      if (timeoutRef.current) {
        clearTimeout(timeoutRef.current)
      }
    }
  }, [])

  return {
    signalRHubConnection: signalRConnection
  }
}

export { useSignalRInitialization }

Expected Behavior

We are looking for reconnection of signalR when the connection is closed due to expiry of the access token. Also,in the documentation we see option called CloseOnAuthenticationExpiration. So, how this can be configured to the serverless azure signal-r ? Ref: https://learn.microsoft.com/en-us/aspnet/core/signalr/configuration?view=aspnetcore-8.0&tabs=dotnet#advanced-http-configuration-options

Also, some browser tab will go into sleep if there are not active (ex: google chrome). In that case how can we ensure the connection with signalR automatically when the browser tab is get back to active.

Version

"@microsoft/signalr": "8.0.0"

Y-Sindo commented 5 months ago

Hi @Manojkumar226, currently serverless mode doesn't have such feature. The feature request is put to our backlog.

Manojkumar226 commented 5 months ago

@Y-Sindo Thank you for the response.

Also, some browser tab will go into sleep if there are not active (ex: google chrome). In that case how can we ensure the connection with signalR automatically when the browser tab is get back to active.

Do you have any suggestions for the above query?

Y-Sindo commented 5 months ago

Also, some browser tab will go into sleep if there are not active (ex: google chrome). In that case how can we ensure the connection with signalR automatically when the browser tab is get back to active.

This is related to SignalR library, please open a separate issue in https://github.com/dotnet/aspnetcore/