Blazor Reconnection Issue After Locking Smartphone

Happy Development 20 Reputation points
2023-06-07T10:57:22.6633333+00:00

I'm experiencing a problem with my Blazor Server application and I need your help. When I access my website on a smartphone and lock the device for some time, upon unlocking it, seems that the Blazor connection gets lost at that moment and tries to reconnect.

I would greatly appreciate it if someone could provide guidance or suggestions to solve this reconnection issue in Blazor after locking and unlocking the smartphone.

Thank you very much for your help!

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,126 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,372 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 54,621 Reputation points
    2023-06-07T15:42:32.4366667+00:00

    it would be nice if the blazor docs made this clearer. Blazor server counts on a persistent connection to the browser client (via signal/r). if the connection closed, the circuit is shutdown (after a brief timeout).

    In addition browsers are getting more aggressive in closing connection (especially on mobile and laptops) to save battery life. the Lock Screen or inactive tab will often close the connection.

    the lost connection recovery must be done in javascript. as you noticed the template just has the reload message. As a true recovery generally requires recovering some state, you need to design you application to be able to recover. A common approach is to store recovery state in the browsers local storage. then you write javascript that catches the lost connection and restarts the app. The simplest is just a reload of the same page (location.reload()). but you might want to add a query parameter so the server knows its restart from saved state.

    see:

    https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/signalr?view=aspnetcore-7.0


  2. AgaveJoe 26,031 Reputation points
    2023-06-09T14:08:34.31+00:00

    What state is supposedly needed to be stored there to avoid this issue?

    Managing state is very common in web development. There always something you want to keep track of and that something depends on how the application works.

    The Counter component in the default Blazor Server template is a good example of state. The currentCount variable holds the user's count. In other words, the state of the current count. The currentCount variable is located in server memory and unique to each browser's SignalR connection. When the SignalR connection is close, Blazor Server will timeout and reclaim the memory so the memory can be used for other things.

    If you wanted to save the state of the count then it is up to you to write code that stores the count somewhere that is not volatile. The next time the user comes online, perhaps after unlocking a phone, the currentCount can be set to the last saved count value.

    As mentioned above, one option is saving the count in local storage.