Search code examples
blazor-server-side.net-5blazor-client-sideasp.net-core-5.0

Blazor Webassembly Database Notification


i am a little overwhelmed by all the Information about Blazor Wasm and Blazor Server. Until now, I have a 5 years old ASP.NET MVC App with a SignalR Hub running to notify my View, when something changed in the Database.

Let's be clear here. I have an existing Database Record. I present this database record to useres in an MVC View. Users are watching this View, and when an external source does an update to this record in the DB, the Partial View refreshes without reloading the whole Website.

As of now, I have a Blazor Webassembly hosted by .NET Core API and could easily switch to Blazor Server.

is this currently possible in Blazor Server/Blazor WASM... I'd prefer WASM if in anyway possible :)

Thanks in advance <3


Solution

  • It's definitely possible with either WASM/Server! You will have to still use a SignalR hub though to tell the client's to refresh the component. However, can be as simple as calling StateHasChanged() when the event to reload the UI is emitted from SignalR Hub. Here's an example:

    ...
    hubConnection.On("RefreshTable", _ =>
    {
        // Retrieve the updated list from the DB - or utilize SignalR?
        StateHasChanged();
    });
    ...
    

    There is a great guide on Microsoft's website to get a basic SignalR chat app running using Blazor WASM: https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr-blazor?view=aspnetcore-5.0&tabs=visual-studio&pivots=webassembly#add-razor-component-code-for-chat