Home > front end >  SignalR disconnecting every few minutes after the tab is backgrounded
SignalR disconnecting every few minutes after the tab is backgrounded

Time:10-22

I'm using SignalR for communication between a Vue.js frontend and dotnet backend.

When you background the frontend tab after a (seemingly random) amount of time the connection gets disconnected. Because I'm using auto reconnect the socket is then reconnected, but then until the tab is re-focused the socket will continue to get disconnected every couple of minutes putting it in a reconnecting loop.

enter image description here

A few things:

  • Seems to happen in all browsers but Firefox
  • When running both the frontend and backend locally, there's no issue
  • Running the frontend locally but backend remotely does cause the issue
  • Neither application are running on IIS
  • Kestrel timeouts have been set to an hour (just in case)
  • The initial disconnect that causes the reconnects to start looping doesn't happen at a consistent time, just somewhere around the 5-10 minute mark
  • No errors are being thrown by either end

Client side code is extremely basic:

 this.connection = new HubConnectionBuilder()
        .withUrl(this.url)
        .withAutomaticReconnect()
        .build()

Server side code is similarly basic:

 public override async Task OnConnectedAsync()
    {
        //Create client code here

        await base.OnConnectedAsync();
    }

    public override async Task OnDisconnectedAsync(Exception exception)
    {
        if(exception != null) _logger.LogErrorToFile(exception.Message);
        _logger.LogInformationToFile($"Client with id: {GetConnectionId()} disconnected.{GetUsername()}");
        await base.OnDisconnectedAsync(exception);
    }

CodePudding user response:

Chrome made an update earlier this year that stops connections when they are no in focus after about 5 minutes. The issue/fix that Nirbhay commented is actually for the ASP.NET version (2.4.2) and not compatible with .NET CORE. Here is the issue that was raised for .NET CORE - github.com/dotnet/aspnetcore/issues/31079

  • Related